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