Rust
In short
Rust is a compiled, statically typed systems language that provides memory safety without a garbage collector by checking ownership rules at compile time.
What is the Rust programming language?
Rust is a general-purpose systems programming language that started as a personal project of Graydon Hoare, was later sponsored by Mozilla, and reached version 1.0 in 2015. Today it is developed by the open-source Rust project with support from the independent Rust Foundation. Its goal is to offer the speed and low-level control of C and C++ while preventing whole classes of memory bugs.
Rust is statically and strongly typed, with extensive type inference. Its memory model is based on ownership: every value has exactly one owner, and the value is freed automatically when its owner goes out of scope. Other code can borrow a value through references, and the compiler's borrow checker enforces the rule that you can have either many readers or one writer at a time, never both. Rust also has no null and no exceptions; missing values use the Option type and recoverable errors use the Result type.
Rust is used for command-line tools, web servers, WebAssembly modules, embedded devices, browser components, and parts of operating systems, including drivers in the Linux kernel. The borrow checker works like a lending library: many people may read a book at the same time, or one person may check it out to write in it, but never both at once, and the library always knows when the book comes back.
A common confusion is how Rust achieves memory safety. Unlike Java or Go, it does not use a garbage collector at runtime; the compiler checks the ownership rules before the program ever runs, so there are no collection pauses. The trade-off is a steeper learning curve, since code that would compile in C may be rejected until ownership is clear. For low-level work the compiler can't verify, Rust offers clearly marked unsafe blocks.
Key takeaways
- Rust compiles to native machine code with performance comparable to C and C++.
- Ownership and borrowing rules give memory safety without a garbage collector.
- The compiler prevents data races in safe code, making concurrency safer.
- Errors are handled with
Resultand missing values withOptioninstead of exceptions andnull.
Example
fn main() {
let greeting = String::from("Hello");
// Borrow the string with & so main keeps ownership
print_twice(&greeting);
let moved = greeting; // Ownership moves to the new variable
// println!("{}", greeting); // Compile error: greeting was moved
println!("{} still works", moved);
}
fn print_twice(text: &str) {
println!("{text} {text}");
}Readers ask
Why is Rust considered memory-safe?
The compiler checks ownership and borrowing rules before the program runs, which rules out common bugs such as use-after-free, double free, and data races in safe code. Low-level operations that can't be checked are limited to clearly marked unsafe blocks.
Is Rust hard to learn?
Rust has a reputation for a steep learning curve, mainly because of ownership and the borrow checker. Its compiler error messages are detailed and often suggest fixes, which helps newcomers.
Does Rust have a garbage collector?
No. Rust frees memory deterministically when a value's owner goes out of scope, so there are no garbage collection pauses.
Often compared
See also
- CProgramming Languages, p. 3C is a compiled, low-level programming language that gives direct control over memory and is used to build operating systems, drivers, and embedded software.
- C++Programming Languages, p. 5C++ is a compiled, statically typed language that extends C with classes and templates while keeping low-level control over memory and performance.
- Memory LeakOperating Systems, p. 19A memory leak is a bug where a program keeps holding memory it no longer needs, so its memory usage grows over time and can slow down or crash the system.
- ConcurrencyProgramming Fundamentals, p. 12Concurrency is a program's ability to make progress on several tasks in overlapping time periods, such as serving many users at once rather than one at a time.
- WebAssemblyWeb Development, p. 59WebAssembly is a compact binary code format that runs in web browsers at near-native speed, letting languages like C++, Rust, and Go power fast web code.
- Garbage CollectionProgramming Fundamentals, p. 23Garbage collection is automatic memory management in which the language runtime finds data a program can no longer use and frees that memory for reuse.
- GoProgramming Languages, p. 13Go is a compiled, statically typed language built for simplicity and fast builds, with garbage collection and built-in concurrency through goroutines.
- ZigProgramming Languages, p. 33Zig is a low-level systems programming language that aims to be a simpler, safer successor to C, with manual memory management and no hidden control flow.
Spotted a mistake or something missing on this page?Suggest an edit