Rust-Cheatsheet
๐ Hello World fn main() {
println!("Hello, world!");
} ๐ก Tip: Use println! macro for output in Rust. ๐ฆ Variables & Data Types fn main() {
let age: i32 = 21;
let pi: f64 = 3.14159;
let grade: char = 'A';
let name: &str = "Maxon";
println!("{} is {} years old.", name, age);
} ๐ก Tip: Variables are immutable by default. Use mut for mutability. ๐ Control Flow fn main() {
for i in 1..=5 {
if i % 2 == 0 {
println!("{} is even", i);
} else {
println!("{} is odd", i);
}
}
} ๐ก Tip: Use if , else if , match , loop , and while for control flow. ๐ Functions fn add(a: i32, b: i32) -> i32 {
a + b
}
fn main() {
println!("Sum: {}", add(5, 3));
} ๐ก Tip: Functions return the last expression automatically (no return needed). ๐ Structs struct Car {
brand: String,
speed: u32,
}
impl Car {
fn drive(&self) {
println!("{}…