rust

rustc is a simple cli to complie rust code cargo is Rust’s build system and package manager.

You aren’t allowed to use mut with constants.

Shadowing is different from marking a variable as mut because we’ll get a compile-time error if we accidentally try to reassign to this variable without using the let keyword. By using let, we can perform a few transformations on a value but have the variable be immutable after those transformations have been completed.

Data Types

Ownership

  • Ownership make Rust memory safety guarantee without garbage collector.

Functions

  • Statements are instructions that perform some action and do not return a value.
  • Expressions evaluate to a resultant value.

Expressions do not include ending semicolons. If you add a semicolon to the end of an expression, you turn it into a statement, and it will then not return a value

fn main() {
    let y = {
        let x = 3;
        x + 1
    };
 
    println!("The value of y is: {y}");
}

Here if x+1 end with a semicolon will raise an error.

Loop

You can also return from inside a loop. While break only exits the current loop, return always exits the current function.