Rust combines low-level control with high-level abstractions. Let’s explore its key features for systems programming.
Memory Safety
fn main() {
// Ownership example
let s1 = String::from("hello");
let s2 = s1; // s1 is moved to s2
// This would cause a compile error:
// println!("{}", s1);
println!("{}", s2); // Works fine
}
Concurrency Without Data Races
use std::thread;
use std::sync::mpsc;
fn main() {
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
tx.send("Hello from another thread!").unwrap();
});
println!("Received: {}", rx.recv().unwrap());
}
Zero-Cost Abstractions
#[derive(Debug)]
struct Point<T> {
x: T,
y: T,
}
impl<T> Point<T> {
fn new(x: T, y: T) -> Self {
Point { x, y }
}
}
Key Benefits:
- Memory safety without garbage collection
- Concurrent programming without data races
- Zero-cost abstractions
- Modern tooling and package management