Rust Syntax
Learn the fundamental building blocks of Rust
📝 Rust Syntax Basics
Rust syntax is designed for clarity and safety. Learn variables, functions, control flow, and data types. Rust's syntax prevents common programming mistakes while maintaining readability and expressiveness for system-level programming tasks.
fn main() {
let name = "Rustacean";
let age: u32 = 25;
let mut score = 100;
println!("Hello, {}! Age: {}, Score: {}", name, age, score);
score += 50;
println!("New score: {}", score);
}
Output:
Hello, Rustacean! Age: 25, Score: 100
New score: 150
Core Syntax Elements
Variables
Immutable by default, mutable with mut
let x = 5; // immutable
let mut y = 10; // mutable
Functions
Defined with fn keyword
fn add(a: i32, b: i32) -> i32 {
a + b
}
Control Flow
if, loop, while, for statements
if x > 0 {
println!("Positive");
}
Data Types
Integers, floats, booleans, chars
let num: i32 = 42;
let pi: f64 = 3.14;
🔹 Variables and Mutability
Rust variables are immutable by default for safety:
fn main() {
// Immutable variable
let x = 5;
println!("The value of x is: {}", x);
// Mutable variable
let mut y = 10;
println!("The value of y is: {}", y);
y = 15;
println!("The value of y is now: {}", y);
// Shadowing
let z = 20;
let z = z + 5; // This creates a new variable
println!("The value of z is: {}", z);
}
Output:
The value of x is: 5
The value of y is: 10
The value of y is now: 15
The value of z is: 25
🔹 Data Types
Rust has several built-in data types:
fn main() {
// Integer types
let small: i8 = 127;
let big: i64 = 1_000_000;
let unsigned: u32 = 42;
// Floating point
let pi: f64 = 3.14159;
let e: f32 = 2.718;
// Boolean
let is_rust_fun: bool = true;
// Character (Unicode)
let letter: char = 'R';
let emoji: char = '🦀';
// String types
let greeting: &str = "Hello"; // string slice
let name: String = String::from("Rust"); // owned string
println!("Numbers: {}, {}, {}", small, big, unsigned);
println!("Floats: {}, {}", pi, e);
println!("Boolean: {}", is_rust_fun);
println!("Characters: {}, {}", letter, emoji);
println!("Strings: {} {}", greeting, name);
}
Output:
Numbers: 127, 1000000, 42
Floats: 3.14159, 2.718
Boolean: true
Characters: R, 🦀
Strings: Hello Rust
🔹 Functions
Functions are defined with the fn keyword:
// Function with parameters and return value
fn add_numbers(a: i32, b: i32) -> i32 {
a + b // No semicolon = return value
}
// Function with no return value
fn greet(name: &str) {
println!("Hello, {}!", name);
}
// Function with multiple return values (tuple)
fn get_name_and_age() -> (String, u32) {
(String::from("Alice"), 30)
}
fn main() {
let sum = add_numbers(5, 3);
println!("Sum: {}", sum);
greet("Rustacean");
let (name, age) = get_name_and_age();
println!("{} is {} years old", name, age);
}
Output:
Sum: 8
Hello, Rustacean!
Alice is 30 years old
🔹 Control Flow
Rust provides several control flow constructs:
fn main() {
let number = 7;
// if expressions
if number < 5 {
println!("Small number");
} else if number < 10 {
println!("Medium number");
} else {
println!("Big number");
}
// if in a let statement
let description = if number % 2 == 0 { "even" } else { "odd" };
println!("The number is {}", description);
// loop
let mut counter = 0;
let result = loop {
counter += 1;
if counter == 3 {
break counter * 2; // break with value
}
};
println!("Loop result: {}", result);
// for loop
let numbers = [1, 2, 3, 4, 5];
for num in numbers {
println!("Number: {}", num);
}
// Range
for i in 1..4 {
println!("Range: {}", i);
}
}
Output:
Medium number
The number is odd
Loop result: 6
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Range: 1
Range: 2
Range: 3