Rust is a systems programming language known for its performance, safety, and concurrency support. Understanding Rust's syntax is crucial for effectively writing safe and efficient code. This tutorial will cover the fundamental aspects of Rust syntax, including variables, data types, control flow, functions, and modules.
Variables in Rust are immutable by default. To make a variable mutable, you need to use the mut keyword.
let x = 5; // Immutable variable
let mut y = 10; // Mutable variable
y = 20;
Rust allows you to shadow variables, which means you can declare a new variable with the same name as an existing one. The new variable will shadow the old one.
let x = 5;
let x = x + 1; // Shadows the previous value of x
Rust is a statically typed language, meaning that it must know the types of all variables at compile time. Rust has two main categories of data types: scalar and compound.
Scalar types represent a single value. Rust has four primary scalar types:
i8, i16, i32, i64, i128 (signed) and u8, u16, u32, u64, u128 (unsigned).f32 and f64.bool (true or false).char, representing a Unicode scalar value.let integer: i32 = 42;
let float: f64 = 3.14;
let boolean: bool = true;
let character: char = 'A';
Compound types can group multiple values into one type. Rust has two primitive compound types:
let tuple: (i32, f64, char) = (500, 6.4, 'z');
let array: [i32; 5] = [1, 2, 3, 4, 5];
Rust provides several control flow constructs similar to other programming languages.
The if statement is used for conditional execution. Rust does not have a ternary operator like some languages; instead, it uses if let for pattern matching.
let number = 3;
if number < 5 {
println!("Condition was true");
} else {
println!("Condition was false");
}
Rust has three types of loops: loop, while, and for.
loop {
println!("This loop runs forever!");
}
let mut number = 3;
while number != 0 {
println!("{}!", number);
number -= 1;
}
println!("LIFTOFF!!!");
for element in array.iter() {
println!("The value is: {}", element);
}
Functions are defined using the fn keyword. Rust functions have explicit return types, which can be inferred or specified.
fn add(a: i32, b: i32) -> i32 {
a + b // The last expression is implicitly returned
}
let sum = add(5, 6);
Closures are anonymous functions that can capture their environment. They are defined using the | syntax.
let closure = |x: i32| x + 1;
println!("Closure result: {}", closure(5));
Rust uses a module system to organize code. Modules help in managing scope and encapsulation.
Modules are defined using the mod keyword. You can nest modules within other modules.
// main.rs
mod my_module {
pub fn public_function() {
println!("This is a public function");
}
fn private_function() {
println!("This is a private function");
}
}
fn main() {
my_module::public_function();
}
You can use modules in other files by declaring them with mod and using the use keyword.
// lib.rs
pub mod my_module;
// main.rs
use crate::my_module::public_function;
fn main() {
public_function();
}
Rust's syntax is designed to provide safety and performance while maintaining flexibility. By understanding the basics covered in this tutorial, you can start writing robust and efficient Rust programs. As you progress, explore more advanced features like lifetimes, traits, and generics to fully leverage Rust's capabilities.