api: YAML JSON TOON Database version: 1.0.0 format: toon dataset: id: 2476 slug: rust-language-features title: Rust Language Features description: "Core Rust programming language concepts: ownership, borrowing, lifetimes, traits, and concurrency." summary: "Core features of Rust by category: ownership, borrowing and lifetimes for memory safety, traits for abstraction, pattern matching, fearless concurrency, Result-based error handling and zero-cost abstractions, with examples." use_when[2]: You are learning Rust's model of ownership or comparing it with garbage-collected languages.,You need an example of an idiomatic Rust feature. caveats[2]: The borrow checker enforces rules at compile time; examples can look simple while hiding lifetime annotations in larger programs.,"Rust editions (2015, 2018, 2021, 2024) introduce syntax changes that are opt-in per crate." related[4]: cpp-modern-features,go-concurrency-patterns,language-comparison-2026,concurrent-data-structures entry_count: 12 fields[4]{section,field,type,example}: features,name,string,Ownership features,category,string,Memory Management features,description,string,Each value has a single owner; when owner goes … features,example,string,"let s1 = String::from(\"hello\"); let s2 = s1; //…" category: Programming Languages category_slug: programming-languages tags: "rust,systems,programming,memory-safety,concurrency" view_count: 41 created_at: 1777673262 updated_at: 1777673262 data: features[12]{name,category,description,example}: Ownership,Memory Management,"Each value has a single owner; when owner goes out of scope, value is dropped.","let s1 = String::from(\"hello\"); let s2 = s1; // s1 is no longer valid" Borrowing,Memory Management,References (&) allow you to refer to some value without taking ownership.,"let r = &s; // immutable borrow\nlet r_mut = &mut s; // mutable borrow (exclusive)" Lifetimes,Memory Management,Annotations ensure all references are valid; compiler enforces lifetime rules.,"fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { if x.len() > y.len() { x } else { y } }" Traits,Abstraction,Define shared behavior; similar to interfaces in other languages. Use generics with trait bounds.,"trait Summary { fn summarize(&self) -> String; }\nimpl Summary for NewsArticle { ... }" Pattern Matching,Control Flow,"Match expressions check enum variants, destructure structs, and bind variables.","match value { Some(x) => x, None => 0, Err(e) => panic!(\"{}\", e) }" Concurrency (Fearless),Concurrency,"Thread-safe by compile-time checks: Send and Sync traits guarantee no data races at runtime.","let handle = thread::spawn(|| { for _ in 0..10 { println!(\"thread\"); } });\nhandle.join().unwrap();" "Error Handling (Result)",Error Handling,"Enums Result and Option force explicit handling; no null or unchecked exceptions.","let f: Result = File::open(\"file.txt\");\nmatch f { Ok(file) => ..., Err(e) => eprintln!(\"{}\", e) }" Zero-Cost Abstractions,Performance,High-level constructs compile to assembly as efficient as hand-written C; no runtime overhead.,"Iterators: v.iter().map(|x| x * 2).filter(|x| x > 10).collect::>();" Enums and Option,Type System,Enums hold one of several variants and can carry data; Option replaces null with an explicit Some or None.,"let x: Option = Some(5);\nmatch x { Some(n) => n, None => 0 };" Macros,Metaprogramming,"Code that writes code, expanded at compile time, such as macro_rules! and derive attributes.","#[derive(Debug, Clone)]\nstruct Point { x: i32, y: i32 }" Iterators and Closures,Functional,Lazy iterator adapters combined with closures compile to loops as fast as hand-written ones.,"let evens: Vec = (1..10).filter(|n| n % 2 == 0).collect();" Smart Pointers,Memory Management,"Types such as Box, Rc and Arc that own heap data and manage sharing and lifetime.","let shared = std::sync::Arc::new(5);"