What is Rust? Ownership, Safety and Zero-Cost Abstractions Explained
In this tutorial, you will learn about What is Rust? Ownership, Safety and Zero. We cover key concepts, practical examples, and best practices to help you master this topic.
Rust is a systems programming language focused on memory safety and concurrency without a garbage collector, using an ownership model with borrowing and lifetime rules enforced at compile time.
What You'll Learn
- The history and design goals of Rust
- Ownership, borrowing, and lifetimes
- Zero-cost abstractions and performance
- Real-world use cases
Why It Matters
Rust provides C-like performance with memory safety guarantees. Firefox uses Rust for its CSS engine (Servo/Stylo). Cloudflare uses Rust for edge computing. Dropbox uses Rust for file synchronization. Durga Antivirus Pro uses Rust for high-performance file scanning. Rust eliminates entire classes of bugs.
Real-World Use
Rust is used for web browsers (Firefox), operating systems (Redox), Embedded Systems, WebAssembly, CLI tools (ripgrep, fd, bat), infrastructure (Cloudflare Workers, Dropbox sync), and game engines.
flowchart LR
A["What is Rust?"] --> B["Installation"]
B --> C["Variables"]
C --> D["Control Flow"]
D --> E["Ownership"]
A:::current --> B
style A fill:#2563eb,stroke:#2563eb,color:#fff
style B fill:#dbeafe,stroke:#2563eb,color:#1e40af
style C fill:#dbeafe,stroke:#2563eb,color:#1e40af
style D fill:#dbeafe,stroke:#2563eb,color:#1e40af
style E fill:#f1f5f9,stroke:#94a3b8,color:#64748b
The History of Rust
Rust began as a personal project by Graydon Hoare at Mozilla Research in 2006. Mozilla began sponsoring it in 2009. Rust 1.0 was released in May 2015, making the first stable release.
Key Milestones
- 2006: Graydon Hoare starts Rust as a personal project
- 2009: Mozilla Research sponsors development
- 2010: First public announcement
- 2015: Rust 1.0 released
- 2018: Rust 2018 edition (NLL, module system improvements)
- 2020: Rust Foundation formed (AWS, Google, Microsoft, Mozilla, Huawei)
- 2021: Rust 2021 edition (closures, Cargo improvements)
- 2024: Rust 2024 edition in development
- 2026: Rust 1.80+ with enhanced async support
Ownership — Rust's Core Innovation
Ownership is Rust's most distinctive feature. It enables memory safety without a garbage collector:
fn main() {
let s1 = String::from("hello");
let s2 = s1; // s1 is MOVED to s2
// println!("{s1}"); // Error! s1 no longer valid
println!("{s2}"); // OK
}
The Rules of Ownership
- Each value has exactly one owner
- The owner goes out of scope, the value is dropped
- Ownership can be transferred (moved)
Borrowing
Instead of transferring ownership, you can borrow references:
fn main() {
let s = String::from("hello");
let len = calculate_length(&s); // Borrow s
println!("'{s}' has length {len}"); // s still valid
}
fn calculate_length(s: &String) -> usize {
s.len()
}
Mutable References
fn main() {
let mut s = String::from("hello");
change(&mut s);
println!("{s}"); // hello, world
}
fn change(s: &mut String) {
s.push_str(", world");
}
Borrowing Rules
- You can have any number of immutable references
- You can have exactly one mutable reference
- References must always be valid (no dangling pointers)
Lifetimes
Lifetimes ensure references are always valid:
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() { x } else { y }
}
Zero-Cost Abstractions
Rust provides high-level abstractions that compile down to efficient machine code — you don't pay for what you don't use:
// Iterator chain — zero-cost compared to hand-written loop
let sum: i32 = (1..1000)
.filter(|n| n % 2 == 0)
.map(|n| n * n)
.sum();
Rust vs Other Languages
| Aspect | Rust | C/C++ | Go | Java |
|---|---|---|---|---|
| Memory | Ownership | Manual | GC | GC |
| Safety | Compile-time | Unsafe | Safe | Safe |
| Performance | Native | Native | Native | JIT |
| Concurrency | Fearless | Complex | Goroutines | Threads |
| Learning curve | High | High | Low | Moderate |
Fearless Concurrency
Rust's type system prevents data races at compile time:
use std::sync::Mutex;
fn main() {
let counter = Mutex::new(0);
// Multiple threads can safely access counter
// Thanks to ownership and Send/Sync traits
}
Common Mistakes
1. Trying to Use a Moved Value
let s1 = String::from("hello");
let s2 = s1;
println!("{s1}"); // Error: s1 was moved
2. Having Both Mutable and Immutable References
let mut s = String::from("hello");
let r1 = &s;
let r2 = &mut s; // Error! Can't have mutable ref with immutable
3. Dangling References
fn dangle() -> &String {
let s = String::from("hello");
&s // Error! s will be dropped
}
4. Forgetting Semicolons
In Rust, semicolons are required for statements. Forgetting them causes confusing errors.
5. Confusing Copy and Move Types
let x = 5;
let y = x; // Copy (integers implement Copy)
println!("{x}"); // OK
let s = String::from("hello");
let t = s; // Move (String doesn't implement Copy)
// println!("{s}"); // Error
Practice Questions
1. What is ownership in Rust?
Every value has exactly one owner. When the owner goes out of scope, the value is dropped. Ownership can be transferred (moved) but not duplicated for non-Copy types.
2. What's the difference between borrowing and ownership?
Borrowing allows temporary access to a value without taking ownership. References (&T and &mut T) borrow the value. The original owner retains ownership.
3. What are lifetimes in Rust?
Lifetimes are compile-time annotations that ensure all references are valid for the duration of their use. They prevent dangling references.
4. What makes Rust memory-safe without a GC?
Rust's ownership system with borrowing, lifetimes, and move semantics is verified at compile time. The compiler enforces rules that prevent use-after-free, double-free, and buffer overflow.
Challenge: Write a Rust function that takes a string slice and returns the first word (up to the first space) using slices.
Solution
fn first_word(s: &str) -> &str {
let bytes = s.as_bytes();
for (i, &item) in bytes.iter().enumerate() {
if item == b' ' {
return &s[..i];
}
}
&s[..]
}
fn main() {
let s = String::from("hello world");
let word = first_word(&s);
println!("First word: {word}"); // hello
}
FAQ
{{< faq question="Is Rust hard to learn?" >}} Rust has a steep learning curve, especially for the ownership system. But once you understand ownership, the compiler catches bugs that would be runtime crashes in other languages. The community is helpful. {{< /faq >}}
{{< faq question="Is Rust good for web development?" >}} Yes. Frameworks like Axum and Actix-web provide high-performance HTTP servers. Rust is also excellent for WebAssembly, powering frontend frameworks like Yew and Leptos. {{< /faq >}}
{{< faq question="What companies use Rust in production?" >}} Mozilla (Firefox), Dropbox, Cloudflare, AWS, Microsoft, Google (Android), Facebook, Figma, Discord, and many more. Rust is especially popular in infrastructure, security, and performance-critical applications. {{< /faq >}}
{{< faq question="Does Rust have a garbage collector?" >}} No. Rust uses its ownership system to manage memory at compile time. This gives predictable performance without GC pauses, making it suitable for real-time and embedded systems. {{< /faq >}}
{{< faq question="Can I use Rust for embedded systems?" >}} Yes. Rust compiles to many architectures including ARM, RISC-V, and wasm. It has excellent embedded support with no_std environments and HALs for microcontrollers. {{< /faq >}}
Try It Yourself
fn main() {
println!("Hello from Rust!");
println!("Rust version: {}", rustc_version());
}
fn rustc_version() -> &'static str {
"1.80.0"
}
Expected output:
Hello from Rust!
Rust version: 1.80.0
What's Next
Now that you understand what Rust is, install it and set up your development environment.
| Topic | Description | Link |
|---|---|---|
| Rust Installation | Set up Rust with rustup | {{< ref "02-installation" >}} |
| Rust Variables | let, mut, types, shadowing | {{< ref "03-variables" >}} |
| Go Features | Compare Go's approach | Go |
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro