openguild-labs / openguild

Official website for OpenGuild community
https://openguild.wtf
MIT License
1 stars 5 forks source link

[Rust Course šŸ¦€] Rust Practices with Rustlings (01-04) #12

Closed chungquantin closed 9 months ago

chungquantin commented 9 months ago
Screenshot 2023-12-28 at 20 20 08

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:

  1. Record a video working through a problem set with instructions from the presenter so the audience can understand and learn Rust from the videos.
  2. Write a README article on how to solve a set of problems in the repository and publish it on https://lowlevelers.com

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.

henchiyb commented 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 šŸ™‡

chungquantin commented 9 months ago

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.

henchiyb commented 9 months ago

@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 šŸ¤” )

henchiyb commented 9 months ago

@chungquantin I put the article here (also created a PR for this): https://github.com/lowlevelers/lowlevelers.com/pull/25 Please review it šŸ™


title: Rust Practices with Rustlings - Introduction and Variables date: 2024-01-03 authors: ['nhan-dnguyen'] tags: ['technical', 'language', 'rust'] draft: true summary: Introduction about Rustlings and the first chapter - variables

Introduction about Rustlings

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!

Chapter 0 - Introduction

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

Chapter 1 - Variables

Exercise 1

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);
}

Exercise 2

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

Exercise 3

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**

Exercise 4

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);
}

Exercise 5

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

Exercise 6

const NUMBER = 3;

fn main() {
    println!("Number {}", NUMBER);
}

Constant in Rust sounds like same as the immutable variable, but it has some different

So 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

chungquantin commented 9 months ago

@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!

chungquantin commented 9 months ago

[x] Finished #25 by @henchiyb

henchiyb commented 9 months ago

@chungquantin Thanks for the review! I created another PR for chapter 2 - functions. https://github.com/lowlevelers/lowlevelers.com/pull/26

chungquantin commented 9 months ago

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.

henchiyb commented 9 months ago

@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

chungquantin commented 9 months ago

@henchiyb yes, feel free to work on those next chapters. There are no one accounted for that yet!