Designed to write fast, correct code for large scale maintable systems and embeddable into other languages
Safety and control
Safe by design, can't have seg fault, null ptrs, and dangling ptrs, or parallel dat race
Enums with match, structs, tuples, options, traits that can be implemented or put in generics
Default immutable and no shared memory, its all opt in with mut and unsafe blocks for everything not allowed
Rust can talk to C, basically directly with ft calls
fn main() {
println!("Hello World!");
}
rustc hello.rs
./hello
Basics Variables and Types?
rust infers types
//Can specify types, but optional
let x: i32 = 42; //i32 good for everything, long in C, int in Java, and dfault
let b: f64 = 3.14; // 64-bit float
let c: bool = true;
let d: char = 'z';
let e: &str = "Hello"; // String slice
let mut s = String::from("hello"); //String object that is mutable
s.push_str(", world!");
let pair: (char, i32) = ('a', 17)
pair.0; //a
pair.1 //17
let (_, r) = slice.split_at(middle)
Lists
let mut numbers: Vec<i32> = Vec::new();
numbers.push(1);
numbers.push(2);
numbers.push(3);
let first = numbers[0]; // Access element
let length = numbers.len(); // Get length
let x = vec![1,2,3,4,5,6,7,8].iter().map(|x| x + 3).fold(0, |x,y| x + y);
Control
// Loop
loop {
println!("This will loop forever");
break; // Exit the loop
}
// While loop
while x < 5 {
println!("x is {}", x);
x += 1;
}
while let Some(value) = some_option.take() {
// Process value
}
// For loop
for i in 0..5 {
println!("i is {}", i);
}
match some_value {
1 => println!("One"),
2 => println!("Two"),
_ => println!("Something else"),
}
//let else
let PATTERN = EXPRESSION else {
// Diverging code block
};
let Ok(count) = u64::from_str(count_str) else {
panic!("Can't parse integer");
}; //count set
let s = String::from("hello");
takes_ownership(s); // s is moved here and then dropped
let x = 5;
let y = &x; // y is a reference to x
let mut z = 5;
let r = &mut z; // r is mutable reference to z
*r += 1;
Concurrency
Threads can communiate by channel
use std::thread;
use std::time::Duration;
fn main() {
let handle = thread::spawn(|| {
println!("Hello from spawn");
});
handle.join().unwrap();
thread::sleep(Duration::from_secs(2));
println!("Main thread ends");
}
Modules and Crates
mod utils {
pub fn greet(name: &str) -> String {
format!("Hello, {}!", name)
}
}
fn main() {
let name = "Alice";
let greeting = utils::greet(name);
println!("{}", greeting);
}