feature ¶
- zero-cost abstractions
- move semantics
- guaranteed memory safety
- threads without data races
- trait-based generics
- pattern matching
Hello, rust ¶
`fn main(){
println!("Hello World");
}
`
`fn main() {
let language = "rust";
println!("Hello, {}", language);
}println!("Hello, {}", language);
`
`fn main() {
println!("Factorial: {}", factorial(5));
}fn factorial(i: u64) -> u64 {
let mut acc = 1;
for num in 2..i+1 {
acc
}for num in 2..i+1 {
acc *= num;
}acc
`Rust ¶
- statement & expression
- statement : no return value
- let statement
- let statement
- expression : evaluate to a resulting value
- operation
- calling a function
- calling a macro
- block
- operation
- statement : no return value
Ownership Rules ¶
- Each value in Rust has a variable that's called its owner.
- There can only be one owner at a time.
- When the owner goes out of scope, the value will be dropped. -> lifetime
- At any given time, you can have either one mutable reference or any number of immutable references.
- References must always be valid.