Rust is a programming language designed with a special focus. What is its main focus?
Think about how Rust handles memory compared to languages like Java or Python.
Rust focuses on memory safety without a garbage collector by using ownership and borrowing rules.
Look at this Rust code snippet. What will it print?
fn main() {
let x = 5;
let y = &x;
println!("{}", y);
}Remember that &x is a reference to x, and println! with {} prints the value.
The code prints the value 5 because y is a reference to x, and println! dereferences it automatically.
What error will this Rust code cause when compiled?
fn main() {
let mut s = String::from("hello");
let r1 = &s;
let r2 = &s;
let r3 = &mut s;
println!("{} {} {}", r1, r2, r3);
}Rust does not allow mutable and immutable references at the same time.
The code tries to borrow `s` as mutable while immutable references exist, which Rust forbids.
Rust uses a special system to ensure safe access to data across threads. What is this system called?
Think about how Rust prevents data races without runtime overhead.
Rust uses ownership and borrowing rules checked at compile time to prevent data races and ensure thread safety.
What does the borrow checker in Rust do?
Consider how Rust prevents bugs related to references before the program runs.
The borrow checker enforces ownership and borrowing rules at compile time to prevent invalid memory access.