Rust is a systems programming language focused on three goals: safety, speed, and concurrency. Rust achieves memory safety without a garbage collector through its unique ownership system. It is used to build operating systems, game engines, web assembly, and high-performance servers.
Rust
Beginner
8 min read
Introduction to Rust
Example
// main.rs
fn main() {
println!("Hello, Rust!");
// Variables are immutable by default
let x = 5;
let mut y = 10; // mutable
y += x;
println!("y = {y}");
// Strings
let s1 = String::from("hello");
let s2 = " world".to_string();
let s3 = s1 + &s2; // s1 is moved here
println!("{s3}");
// Arrays and vectors
let arr = [1, 2, 3, 4, 5];
let vec: Vec<i32> = vec![1, 2, 3];
// Iteration
for num in &arr {
print!("{num} ");
}
println!();
// Match expression
let number = 7;
match number {
1 => println!("one"),
2..=5 => println!("two to five"),
_ => println!("something else"),
}
}