Introduction
Rust combines several distinctive features that set it apart from other systems languages: memory safety without garbage collection, zero-cost abstractions, fearless concurrency, and a strong static type system. Instead of null references, Rust uses the Option<T> type, and instead of exceptions, it uses the Result<T, E> type for recoverable errors. These design choices push many classes of bugs to compile time rather than runtime.
Cricket analogy: Rust's memory safety without garbage collection is like a scorer who tallies every run in real time without stopping play to recount, while Option<T> replaces the old habit of guessing an unrecorded 'null' score.
Syntax
fn divide(a: i32, b: i32) -> Result<i32, String> {
if b == 0 {
Err(String::from("division by zero"))
} else {
Ok(a / b)
}
}Explanation
The function above returns a Result<i32, String>, which is either Ok holding a valid integer or Err holding an error message. Callers are forced by the type system to handle both possibilities, which prevents unchecked error conditions from silently causing crashes. Similarly, Option<T> represents a value that may or may not be present (Some(value) or None), eliminating null-pointer errors common in other languages. Rust's ownership rules also let the compiler verify at build time that data is never accessed after being freed and that multiple threads cannot mutate shared data unsafely, enabling 'fearless concurrency.'
Cricket analogy: A function returning Result<i32, String> is like a run-chase calculator returning either Ok(target_runs) or Err('rain interrupted'), forcing the scoreboard app to handle both outcomes instead of crashing on a washout.
Example
fn divide(a: i32, b: i32) -> Result<i32, String> {
if b == 0 {
Err(String::from("division by zero"))
} else {
Ok(a / b)
}
}
fn main() {
match divide(10, 2) {
Ok(value) => println!("Result: {}", value),
Err(e) => println!("Error: {}", e),
}
}Output
Result: 5Key Takeaways
- Rust achieves memory safety without a garbage collector through ownership and borrowing.
- Zero-cost abstractions mean high-level constructs compile to efficient machine code with no extra runtime cost.
- Option<T> replaces null references, forcing explicit handling of the 'no value' case.
- Result<T, E> replaces exceptions for recoverable errors, making error handling explicit in function signatures.
- Rust's type system and borrow checker enable fearless concurrency by catching data races at compile time.
- Rust is statically and strongly typed, catching many bugs before the program runs.
Practice what you learned
1. What does Rust use instead of null references?
2. What does Rust use instead of exceptions for recoverable errors?
3. What term describes Rust's ability to write multi-threaded code with compile-time safety guarantees?
4. What does 'zero-cost abstraction' mean in Rust?
Was this page helpful?
You May Also Like
Introduction to Rust Programming
A beginner-friendly overview of Rust, a systems programming language built for memory safety and speed.
Ownership in Rust
Rust's core memory-safety system where every value has exactly one owner and is dropped when that owner goes out of scope.
The Option Type in Rust
Understand how Rust's Option<T> enum replaces null to represent optional values safely.
Error Handling in Rust (Result)
Learn how Rust models recoverable errors with the Result enum and the ? operator instead of exceptions.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics