Rust

From WikiTech, the encyclopedia of technology

Technology · Programming & Languages · 5 min read · 1,001 words · Updated

Rust
Technology
Rust — article photograph or illustration
Image placeholderPlace the file shown below in the assets/articles folder
File: rust.webp
Designed byGraydon Hoare
First stable release1.0, 15 May 2015
ParadigmsMulti-paradigm, systems
TypingStatic, strong, inferred
Websiterust-lang.org

Rust is a general-purpose, statically typed, compiled programming language designed for performance and memory safety. It targets the same domain as C and C++ but checks memory and thread-safety rules at compile time.

History

Graydon Hoare began Rust as a personal project in 2006. Mozilla sponsored it from 2009, and version 1.0 was released on 15 May 2015. The Rust Foundation, an independent non-profit, was established in 2021.

Architecture

The compiler (rustc) uses LLVM to generate machine code. The type system tracks ownership: each value has one owner, and when the owner goes out of scope the value is dropped. References borrow values and are checked by the borrow checker, so a program cannot hold a mutable reference alongside other references to the same data.

fn main() {
    let s = String::from("hello");
    let t = s;            // ownership moves to t
    // println!("{}", s); // compile error: s was moved
    println!("{}", t);
}

Key mechanisms

Key features include enums with pattern matching, traits for generic programming, zero-cost abstractions, and Result and Option types for error handling instead of exceptions. The unsafe keyword marks code where the programmer takes responsibility for upholding invariants.

Adoption

Rust is used in browsers, operating system components, command-line tools and cloud infrastructure. It is an official language option for new code in the Linux kernel since version 6.1.

Ecosystem

Cargo is the build tool and package manager, and crates.io is the package registry.

Comparison with alternatives

Compared with C++, Rust provides memory safety by default. Compared with Python, it requires more explicit design but runs without an interpreter.

Limitations

The learning curve of ownership and lifetimes is steep, and compile times for large projects are long.

Ownership and borrowing in more detail

Rust's ownership model is designed to make memory management predictable without requiring a tracing garbage collector. Each value has one owner, and ownership can move from one variable to another. When the owner leaves scope, Rust runs the value's destructor and releases its resources. The compiler checks these rules so that many use-after-free and double-free errors are rejected before the program runs.

Borrowing allows code to use a value without taking ownership of it. Shared references permit reading, while a mutable reference permits controlled mutation. The compiler checks that references do not outlive the data they point to and that conflicting access is prevented. These restrictions can be unfamiliar at first, but they make data ownership and mutation visible in the program's structure.

Lifetimes express relationships between references. In many cases the compiler can infer them, but function signatures sometimes need explicit lifetime annotations to describe how returned references relate to input references. A lifetime annotation does not extend an object's lifetime; it documents and checks a relationship that must already be valid.

Types, traits and error handling

Rust uses enums and pattern matching to model alternatives explicitly. The Option type represents a value that may be absent, while Result represents an operation that may succeed or fail. This makes potential failure visible in function signatures and encourages callers to handle it. The ? operator provides concise propagation of errors when a function returns a compatible Result or Option type.

Traits define shared behaviour and support generic programming. A type can implement a trait to provide the required methods, and generic functions can state which capabilities their type parameters must provide. The compiler can monomorphise many generic functions, producing specialised code for concrete types. This often allows abstraction without the runtime overhead associated with some dynamic-dispatch approaches.

Concurrency and unsafe code

Rust's type system includes rules that help prevent data races in safe code. Types such as Send and Sync describe whether values may be transferred between threads or safely shared. Synchronisation primitives such as mutexes and channels are available when concurrent code needs coordination. These tools do not make concurrency automatically simple, but they make many incorrect combinations harder to express.

The unsafe keyword permits operations that the compiler cannot fully verify, such as raw pointer dereferencing or certain foreign-function interfaces. Unsafe code is sometimes necessary for low-level libraries and performance-sensitive components. It does not turn off every Rust check; instead, it marks a boundary where programmers must uphold additional invariants and document why the operations are valid.

Toolchain and ecosystem

Cargo manages builds, dependencies, tests and package publishing. Crates.io hosts packages called crates, and the Rust compiler can target many operating systems and processor architectures. The ecosystem includes command-line tools, web services, embedded development, game technology and systems libraries. The standard library provides common collections, networking, filesystem and concurrency features, while external crates extend these capabilities.

The compiler's diagnostic messages are a notable part of the learning experience. They often explain why a borrow or type rule was rejected and suggest how to restructure the code. Developers still need to understand the underlying ownership model rather than blindly applying suggested changes.

Adoption and trade-offs

Rust is attractive for software where performance, reliability and control over resources are important. It can be used in new components of large systems, command-line utilities and services where memory safety matters. Its growing use in operating-system work demonstrates that a modern language can coexist with long-established low-level code.

The learning curve is real. Generic types, lifetimes and trait bounds can make some designs verbose, and compile times may be significant in large projects. The language also does not prevent every logic error, resource exhaustion problem or security flaw. Rust provides strong tools for avoiding certain classes of bugs, but engineering review, testing and secure design remain necessary.

Editorial connections and engineering implications

Rust's safety guarantees apply to safe code under the language rules. Low-level tasks such as raw-pointer operations and foreign-function interfaces may require unsafe blocks, which mark obligations the compiler cannot verify. unsafe does not make an operation safe; it makes the boundary explicit. Good projects keep such code small, document invariants and expose safe interfaces to the rest of the program.

Rust can interoperate with C libraries, which helps teams adopt it incrementally. Ownership, allocation and error conventions must be clear at the boundary, especially when data crosses language runtimes. Cargo provides a consistent build and dependency workflow, but third-party packages still need maintenance and security review. Rust is often introduced for new parsers, services or libraries rather than by rewriting an entire mature system. It can coexist with C and C++ and is increasingly used in selected Linux components.

See also

  • C (programming language) – A general-purpose, statically typed systems programming language created at Bell Labs in the early 1970s.
  • C++ – A general-purpose, statically typed language that extends C with classes, templates and zero-overhead abstractions.
  • Garbage Collection – Automatic memory management in which a runtime reclaims memory that a program can no longer reach.
  • Python – A high-level, dynamically typed programming language that emphasises readability.
  • Linux – A family of open-source, Unix-like operating systems based on the Linux kernel.
  • Grace Hopper – American computer scientist and United States Navy rear admiral who developed an early compiler.
  • TypeScript – A language that extends JavaScript with a static type system and compiles to ordinary JavaScript.
  • WebAssembly – A portable binary instruction format that runs at near-native speed in browsers and other runtimes.

References

  1. ↑ The Rust Programming Language (rust-lang.org)