Rust Programming Language Documentation

website·Indexed 29 days ago

https://rs.hyperter.top

Comprehensive guides and tutorials on Rust, covering core concepts like ownership, borrowing, lifetimes, and traits. Includes advanced topics such as macro programming, multithreading with Send and Sync, smart pointers (Box, Rc, RefCell), and design patterns for efficient memory management and concurrency.

Tokens
22.9K
Snippets
115
Records
142
Agent score
47%

What's inside Rust

  1. Overview of the Zig Toolchain and Build System

    Zig provides a complete toolchain beyond its programming language. This includes zig cc and zig c++ for C/C++ compilation, and zig build, which serves as the build system for Zig, C, and C++ projects.
    # C/C++ Compiler
    zig cc
    zig c++
    
    # Zig/C/C++ Build System
    zig build
  2. Overview of Smart Pointers in Rust

    Smart pointers are data structures that behave like pointers but include additional metadata (such as reference counts). Unlike regular references, which only borrow data, smart pointers own the data they point to. To function as a smart pointer, a struct must implement the Deref and Drop traits:

    • Deref: Allows the smart pointer to be treated like a reference, enabling code to work with both references and smart pointers.
    • Drop: Allows custom logic to execute when the smart pointer goes out of scope (e.g., cleaning up memory).
  3. Overview of Rust language features

    Rust is designed to empower developers to write confident code across a wide range of programming domains. Its core strengths include:

    • High Performance: Extremely fast and memory-efficient. It requires no runtime or garbage collector, making it suitable for performance-critical services, embedded devices, and integration with other languages.
    • Reliability: Uses a rich type system and an ownership model to guarantee memory safety and thread safety, eliminating many classes of errors at compile time.
    • Productivity: Provides a friendly compiler with helpful error messages, comprehensive documentation, and a first-class toolchain including an integrated package manager, build tool, automatic formatter, and smart editor support with autocomplete and type checking.
  4. Understand the difference between Rust macros and functions

    Macros differ from functions in two primary ways:

    1. Variable Arguments: While Rust functions have fixed signatures, macros can accept a variable number of arguments (e.g., println!).
    2. Expansion Time: Macros are expanded into code by the compiler before the code is interpreted. This allows macros to implement traits for types at compile time, whereas functions are called at runtime and cannot define traits.
  5. Use integer and floating-point types

    Rust provides several scalar types for numbers:

    Integers: Available in signed (i) and unsigned (u) variants for 8, 16, 32, 64, and 128 bits. isize and usize depend on the computer's architecture (32 or 64 bit). The default integer type is i32.

    Floats: f32 and f64 (default). All floats are signed and follow the IEEE-754 standard.

    Literals: You can use underscores as separators (e.g., 1_000) and type suffixes (e.g., 57u8).

    let a: u8 = 255;       // Unsigned 8-bit
    let b: i8 = -128;       // Signed 8-bit
    let c: f64 = 2.0;       // Double precision float
    let d = 0b1111_0000;    // Binary literal
    let e = 0xff;           // Hex literal
  6. Understand Stack vs Heap memory allocation in Rust

    Rust uses two primary memory locations: the Stack and the Heap.

    The Stack: Stores data with a known, fixed size. It operates in a Last-In-First-Out (LIFO) manner. Allocation is fast because the OS does not need to search for available space; it simply pushes data onto the top of the stack.

    The Heap: Used for data where the size is unknown at compile time or may change. When memory is allocated on the heap, the OS finds a large enough space and returns a pointer. This pointer (which has a fixed size) is then stored on the stack. Accessing heap data is slower than stack data because it requires following the pointer to the actual memory location.

  7. Understand Rust Ownership rules

    Rust manages memory through an ownership system that the compiler checks at compile time. This eliminates the need for a garbage collector or manual memory allocation/deallocation.

    The three core rules of Ownership:

    1. Every value in Rust has a variable called its owner.
    2. There can only be one owner at a time.
    3. When the owner goes out of scope, the value will be dropped (memory is automatically released via the drop function).

    For simple types (like i32), assignment performs a copy. For heap-allocated types (like String), assignment performs a move, transferring ownership to the new variable and making the previous owner invalid.

    fn main() {
        let s1 = String::from("value");
        let s2 = s1; // Ownership moves from s1 to s2
        // println!("{s1}"); // Error: borrow of moved value
    }
    
    fn main() {
        let s1 = String::from("value");
        let s2 = s1.clone(); // Explicitly clone the data to keep both owners
        println!("{s1}"); // Works
    }
  8. Use Rust for WebAssembly (Wasm) development

    Rust can be used to enhance JavaScript applications by creating WebAssembly modules. These modules can be published to npm and bundled using webpack for integration into web projects.
  9. Understand Send and Sync marker traits

    Send and Sync are marker traits used by the Rust compiler to ensure thread safety during concurrency:

    • Send: A type implementing Send can safely transfer its ownership between threads.
    • Sync: A type implementing Sync can be safely shared between threads via references (&T).

    Relationship: A type T is Sync if and only if its reference &T is Send.