Rust Cheat Sheet
dev.to·2h·
Discuss: DEV

1. SETUP:

# Install Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# Check version
rustc --version

# Create new project
cargo new my_project
cd my_project

# Build and run
cargo build
cargo run


2. BASICS:

fn main() {
println!("Hello, world!"); // Print
let x = 5;                  // Immutable variable
let mut y = 10;             // Mutable variable
const PI: f64 = 3.1415;     // Constant
}


3. DATA TYPES:

// Scalar
let a: i32 = -42;
let b: u32 = 42;
let c: f64 = 3.14;
let d: bool = true;
let e: char = 'R';

// Compound
let tup: (i32, f64, char) = (500, 6.4, 'z');
let (x, y, z) = tup; // Destructuring

let arr = [1, 2, 3, 4, 5];
let slice: &[i32] = &arr[1..3];


4. CONTROL FLOW:

if x > 5 {
p...

Similar Posts

Loading similar posts...