Rust Basic Syntax

Essential Rust syntax and program structure

Program Structure

fn main() { # entry point
    println!("Hello World!"); # output to console
}

Variables

let x = 5; # immutable variable
let mut y = 10; # mutable variable
y = 20; # can be changed
const MAX: i32 = 100; # constant value

Data Types

i8, i16, i32, i64, i128 # signed integers
u8, u16, u32, u64, u128 # unsigned integers
f32, f64 # floating point
bool # true or false
char # Unicode character
String # growable string
&str # string slice

Type Annotations

let x: i32 = 5; # explicit type
let price: f64 = 99.99;
let name: String = String::from("John");

Comments

// single line comment
/* multi-line
   comment */
/// documentation comment