YouTip LogoYouTip

Rust Intro

Rust is a systems programming language led by Mozilla Research, with **memory safety**, **zero-cost abstractions**, and **fearless concurrency** as its core design goals. Rust was born in 2006 as a personal project of Mozilla employee Graydon Hoare, and was officially sponsored by Mozilla in 2009 with a team assembled for development. Rust's original design goal was to solve the long-standing pain points of C/C++ in systems programming: memory safety issues (null pointers, dangling pointers, buffer overflows) and the complexity of concurrent programming (data races, deadlocks). !(#) > Rust's design philosophy is "eliminate bugs at compile time"β€”through a powerful type system and ownership mechanism, the compiler helps you check for memory errors and data races, rather than waiting until runtime to discover problems. The Rust language mascot is a crab named Ferris, friendly and cute, representing the Rust community's friendly and inclusive culture. Rust's key design goals can be summarized as follows: | Design Goal | Description | | --- | --- | | Memory Safety (No GC) | Memory safety is guaranteed at compile time through the ownership system, without a garbage collector | | Zero-Cost Abstractions | High-level language features have no runtime overhead after compilation, with performance consistent with hand-written low-level code | | Fearless Concurrency | Type system and ownership rules eliminate data races at compile time | | Practicality | Can write operating system kernels as well as Web applications, covering full-stack scenarios | | Strong Type Inference | The compiler automatically infers most types, reducing boilerplate code | | Rich Toolchain | Cargo (package management + build), rustfmt (formatting), clippy (linting) out of the box | * * * ## Rust Language Development History From a personal project to a globally adopted systems language, Rust has undergone nearly twenty years of development. | Time | Version/Event | Description | | --- | --- | --- | | 2006 | Personal Project | Graydon Hoare began designing the Rust language | | 2009 | Mozilla Sponsorship | Mozilla officially supported Rust development and formed a team | | 2010 | First Public Release | Rust compiler (written in OCaml) was first publicly disclosed at Mozilla Summit | | 2011 | Self-hosting Complete | Rust compiler was rewritten in Rust itself, freeing itself from OCaml dependency | | May 2015 | Rust 1.0 | First stable version released, marking the language officially ready for production use | | December 2018 | Rust 2018 Edition | First Edition released, optimizing syntax and module system (NLL borrow checker landed) | | 2019 | Async Ecosystem Matures | async/await syntax stabilized, async runtimes like Tokio matured | | 2021 | Rust Foundation Established | AWS, Google, Huawei, Microsoft, and Mozilla jointly established the Rust Foundation | | October 2021 | Rust 2021 Edition | Introduced improvements like disjoint capture, IntoIterator for arrays | | 2022 | Linux Kernel Adoption | Linux 6.1 officially merged Rust support, Rust became the second language for kernel development | | February 2024 | Rust 2024 Edition | Further optimized language consistency and developer experience | > The establishment of the Rust Foundation in 2021 was a milestone. Five tech giantsβ€”AWS, Google, Huawei, Microsoft, and Mozillaβ€”jointly committed to ensuring Rust's long-term independent development, eliminating community concerns about "single-company dominance." Rust follows a six-week stable release cadence, while handling incompatible language changes through the Edition mechanism (released approximately every three years). The same project can mix crates from different Editions, with the compiler ensuring backward compatibility. * * * ## Core Features of Rust Language Rust's feature set revolves around three dimensions: "safe, efficient, and practical." Below, we break down Rust's most core language features one by one. ### Ownership System Ownership is Rust's most unique and important feature. It manages memory at compile time, without a garbage collector or manual malloc/free. The ownership system has three rules: | Rule | Description | Implication | | --- | --- | --- | | Each value has exactly one owner | A value can only be owned by one variable at a time | Prevents double-free and dangling pointers | | When owner leaves scope, value is released | When a variable leaves its scope, Rust automatically calls drop to free memory | No manual memory management required | | At any given time: either one mutable reference, OR multiple immutable references | Write-read mutually exclusive, read-read coexist | Compile-time elimination of data races | Intuitive feeling at the code level: when a variable is assigned to another variable, ownership is transferred (move), and the original variable becomes invalid. ## Example fn main(){ // s1 gets ownership of the string (data stored on the heap) let s1 = String::from("Hello, TUTORIAL!"); // Ownership transfers from s1 to s2, after which s1 is no longer valid let s2 = s1; // println!("{}", s1); // Compile error! s1 has been moved println!("{}", s2);// Correct: s2 is now the owner // Primitive types (stored on the stack) implement the Copy trait, no move occurs let x =42; let y = x;// x is still valid because i32 implements Copy println!("x = {}, y = {}", x, y);// Both can be used // Immutable references: multiple can exist simultaneously let s3 =&s2;// Immutable borrow let s4 =&s2;// Another immutable borrow, allowed println!("s3 = {}, s4 = {}", s3, s4); // Mutable reference: only one allowed at a time let mut data = String::from("tutorial"); let r =&mut data;// Mutable borrow r.push_str("!");// Modify original value through mutable reference println!("After modification: {}", r); // s3 and s4's scopes have ended, so mutable reference can be created } > The ownership system is the "steep hill" in the Rust learning curve. Beginners often "fight" with the compiler, but once you understand it, you'll find that those errors caught by the compiler might be fatal runtime bugs in C/C++. ### Pattern Matching Rust's match expression is extremely powerfulβ€”the compiler performs exhaustive checking on all cases, ensuring no branches are missed. ## Example fn main(){ // match with number matching let score =85; match score { 90..=100=> println!("TUTORIAL Rating: A"),// Range matching 60..=89=> println!("TUTORIAL Rating: B"), 0..=59=> println!("TUTORIAL Rating: C"), _ => println!("Invalid score"),// Wildcard, matches all remaining cases } // match destructuring tuples let pair =(3,7); match pair { (0, y)=> println!("First is 0, second is {}", y), (x,0)=> println!("First is {}, second is 0", x), (x, y)=> println!("Two values: {} and {}", x, y), } } ### Enums and Option/Result Rust's enums can carry data, and when used with match, they enable safe and expressive control flow. The standard library's Option and Result are the two most core enum types, handling "possibly empty" and "possibly erroneous" scenarios respectivelyβ€”Rust has no null or exceptions. ## Example use std::fs; fn main(){ // Option: value may or may not exist (alternative to null) let numbers = vec![10,20,30]; let first = numbers.get(0);// Returns Option let missing = numbers.get(5);// Returns Option match first { Some(&val)=
← Dart TutorialVue3 Introduction β†’