《Rust Programming Language》- 01 - Getting Started
Instead of saving the result of the build in the same directory as our code, Cargo stores it in the target/debug directory.>
Command | Desc |
cargo new | Create a project |
cargo build | Build a project |
cargo build --release | Build release ver |
cargo run | Run a project |
cargo check | build a project without producing a binary to check for errors using cargo check |
-
For same variable name, the former is shadowed by the latter
fn main() { let x = 5; let x = x + 1; { let x = x * 2; println!("The value of x in the inner scope is: {x}"); } println!("The value of x is: {x}"); // prints 6 }
-
A shadowed variable can change its type.
fn main() { let spaces = "123123123"; let spaces = spaces.len(); /* type changed */ println!("{spaces}"); }
-
To reassign the variable, you must use
let
keywordfn main() { let x = 5; x = x + 1; // Error, no let keyword on reassign }
-
The keyword
let
allows for type changes, whilelet mut
disallows them.fn main() { let mut spaces = " "; spaces = spaces.len(); /* Error: Disallow type change for mutable */ }
Rust is a statically-typed language. This means that all variable types must be known at compile time.
#![allow(unused)] fn main() { /* type annotation (u32) is required */ let guess: u32 = "42".parse().expect("Not a number!"); }
Rust has two data type subsets: scalar and compound
- Integer
- Integer Literals
- Floating-Point
- Numeric
- Boolean
- Character
- Tuple
- Array
- You can define functions anywhere within the scope of the caller, before or after the function call.
- Parameter type annotation is required.
fn main() { print_labeled_measurement(5, 'h'); } fn print_labeled_measurement(value: i32, unit_label: char) { println!("The measurement is: {value}{unit_label}"); }
fn main() { let x = (let y = 6); // Error: Statement `let y = 6` do not have return value }
Expressions (which may return a value) do not include ending semicolons. Adding a semicolon to the end of an expression turns it into a statement that does not return a value.
fn main() { let y = { let x = 3; x + 1 // Attention: No semicolon, means this is a expression }; // returns 4 println!("The value of y is: {y}"); }
fn main() { let x = plus_one(5); println!("The value of x is: {x}"); } fn plus_one(x: i32) -> i32 { x + 1 // Attention: no semicolon, express with return value }
-
if
Must have boolean typefn main() { let number = 12; if number % 4 == 0 { // only boolean type println!("number is divisible by 4"); } else if number % 3 == 0 { println!("number is divisible by 3"); } else { println!("number is not divisible by 4, 3, or 2"); } }
-
Use
if
in alet
Statement, but the return values must in same typefn main() { let condition = true; // Do not work because return values are not in same type // let number = if condition { 5 } else { "six" }; let number = if condition { 5 } else { 6 }; println!("The value of number is: {number}"); }
-
Traditional
loop
fn main() { let mut counter = 0; let result = loop { /* loop in statement */ counter += 1; if counter == 10 { break counter * 2; /* send the break value as return value */ } }; println!("The result is {result}"); }
-
While
Loopfn main() { let mut number = 3; while number != 0 { println!("{number}!"); number -= 1; } println!("LIFTOFF!!!"); }
-
for...in
loopfn main() { // 1..10 is a Range from 1 to 9 // 1..=10 is a Range from 1 to 10 // (1..=10).rev() is a Range from 10 to 1 for number in 1..=10 { println!("{number}!"); } println!("DONE!!!"); }
留言板
PLACE_HOLDER
PLACE_HOLDER
PLACE_HOLDER
PLACE_HOLDER