Closed chungquantin closed 9 months ago
@chungquantin Hi Chung, Thanks for founding the LowLever community and sharing your knowledge! I'm starting to learn Rust and starting with Rustlings, I'd like to take this issue š
Solve method: Write a README article
Time expected to finish: 3-4 days from today
btw related to the README article, I guess that should be a blog post on lowlevers.com, under the data/blog/rust/
right?
If you have any concerns please let me know š
Hey @henchiyb, thanks for your kind words. I will assign this issue to you. šŖ For now, could you please send a README article directly to this issue, I will help to review and bring that to the website. I am building the process of publishing blogs. Hope we will have a better pipeline later.
@chungquantin is it better to create a PR for the README (blog post) and you can review it on the PR? (I'm not sure about how to public the blog post on the website, but I imagine that you only need to merge the PR and deploy š¤ )
@chungquantin I put the article here (also created a PR for this): https://github.com/lowlevelers/lowlevelers.com/pull/25 Please review it š
Rustlings is a project that contains small exercises to get you used to reading and writing Rust code. This includes reading and responding to compiler messages!
GitHub Repository: https://github.com/rust-lang/rustlings
The book (for using when learning Rust with Rustlings - include some hint from rustlings): https://doc.rust-lang.org/book/title-page.html
For more details about Rustlings and how to use it, please visit Rustlings GitHub page!
With the introduction of Rustlings, we learn how to use the Rustlings and how to write the very first Hello World on rust
// The original code:
fn main() {
println!("Hello {}!");
}
You need to find out the problem and fix it. we can easily see in the code above we have {} - that is used for print something, and we need to pass a value into it to print out.
//Solution
fn main() {
println!("Hello {}!", "Rust");
}
You can also pass a variable into it, and more
let name = "Rust";
println!("Hello {}!", name);
//Result; Hello Rust
println!("Hello {1}, {1} is learning {0}", "Rust", "Bob")
// Result; Hello Bob, Bob is learning Rust
fn main() {
x = 5;
println!("x has the value {}", x);
}
Rust uses the let command to define a new variable, so the one is missing here is let command. Easy!
fn main() {
let x = 5;
println!("x has the value {}", x);
}
fn main() {
let x;
if x == 10 {
println!("x is ten!");
} else {
println!("x is not ten!");
}
}
Same as other programming languages, we need to assign an init value to x before we can use the variable.
fn main() {
let x = 10;
if x == 10 {
println!("x is ten!");
} else {
println!("x is not ten!");
}
}
We can also do it in another way
let x;
x = 11
fn main() {
let x: i32 = 10;
println!("Number {}", x);
}
Quite the same as the exercises above, we need to assign a value for the variable
1 thing here is the variable's type is i32
, which means we need to assign an integer value for that variable.
You can try assigning an float variable to see the error
fn main() {
let x: i32 = 10.1;
println!("Number {}", x);
}
// Result: **^^^^** **expected `i32`, found floating-point number**
fn main() {
let x = 3;
println!("Number {}", x);
x = 5; // don't change this line
println!("Number {}", x);
}
In Rust we have 2 types of variables: mutable and immutable. For immutable variables, we cannot change the value of the variable. In the example above the error will be "cannot assign twice to immutable variable" We can make the variable mutable to re-assign a new value to it
fn main() {
let mut x = 3;
println!("Number {}", x);
x = 5; // don't change this line
println!("Number {}", x);
}
You cannot assign a value that has different type than the first value. Example this example below will raise the error: "^ expected floating-point number, found integer"
fn main() {
let mut x = 3.1;
println!("Number {}", x);
x = 5; // don't change this line
println!("Number {}", x);
}
fn main() {
let number = "T-H-R-E-E"; // don't change this line
println!("Spell a Number : {}", number);
number = 3; // don't rename this variable
println!("Number plus two is : {}", number + 2);
}
The same as the exercise 4, we cannot change the type of the variable when we re-assign a new value. What should we do? In Rust we have a thing called Shadowing. It allows us to re-defined the variables (including their types and values).
fn main() {
let number = "T-H-R-E-E"; // don't change this line
println!("Spell a Number : {}", number);
let number = 3; // just add the 'let' word
println!("Number plus two is : {}", number + 2);
}
Shadowing affects block scope. Example:
fn main() {
let number = 3;
let number = number + 1;
{
let number = number + 2;
println!("Number in block is: {}", number);
}
println!("Number plus one is: {}", number);
}
// Result:
// Number in block is: 6
// Number plus one is: 4
const NUMBER = 3;
fn main() {
println!("Number {}", NUMBER);
}
Constant in Rust sounds like same as the immutable variable, but it has some different
mut
with constSo for this exercise, we only need to add a type annotation for the const
const NUMBER: i32 = 3;
fn main() {
println!("Number {}", NUMBER);
}
The first chapter of Rustlings - Variables ends here. TIL:
Thanks for reading and please add comments below if you have any questions
@henchiyb this looks awesome, I will put this on the website. Because I want this article to be under a Rustling course
page so need extra effort for the front end. Thanks for your contribution!
[x] Finished #25 by @henchiyb
@chungquantin Thanks for the review! I created another PR for chapter 2 - functions. https://github.com/lowlevelers/lowlevelers.com/pull/26
Hey @henchiyb, aside from publishing the README article to lowlevelers.com
what do you think if I create a repository called rustlings-guide
that contains all the blogs you wrote and your Rust code? Like a fork of Rustlings.
@chungquantin SGTM! Just create it š btw I created a PR for chapters 3 and 4 here: https://github.com/lowlevelers/lowlevelers.com/pull/27 I'd pick the next issue for chapters 5 to 8 if no one has picked it yet
@henchiyb yes, feel free to work on those next chapters. There are no one accounted for that yet!
Description
Solving exercises in Rustlings repository and delivers your way of thinking + solutions to the community members. To get assigned, please mention your name in the comment section below with the expected time to finish the issue.
Issue Guidelines
There are two ways of working on this issue, you can choose one of the following:
Please read this information first before working on this issue
Before committing to the tasks in the community, please skim through the guidelines below to grasp the overall idea of how the community works first. It does not take long but I believe it will give you a big picture of the vision and culture of TheLowLevelers.