Programming Language
Rust is a multi-paradigm, general-purpose programming language designed for performance and safety, especially safe concurrency. Rust is syntactically similar to C++, but provides memory safety without using garbage collection. It achieves memory safety through a novel ownership system with a compiler that enforces rules at compile time.
Rust was originally designed by Graydon Hoare at Mozilla Research and is now governed by the Rust Foundation. It consistently ranks as the "most loved programming language" in the Stack Overflow Developer Survey since 2016.
// Hello World program fn main() { println!("Hello, world!"); // Basic variable declaration let x: i32 = 42; // immutable by default let mut y: i32 = 10; // mutable y = 20; // OK because y is mutable // Type inference let z = 3.14; // f64 inferred // Tuples let tuple: (i32, f64, bool) = (42, 3.14, true); println!("{:?}", tuple); // Arrays let array: [i32; 5] = [1, 2, 3, 4, 5]; println!("Array: {:?}", array); }
fn main() { // Ownership example let s1 = String::from("hello"); let s2 = s1; // s1 is moved to s2, s1 is no longer valid // Uncomment to see error: // println!("{}", s1); // ❌ Error: value borrowed after move println!("{}", s2); // This works // Borrowing with references let s3 = String::from("hello world"); // Immutable borrow let len = calculate_length(&s3); println!("The length of '{}' is {}.", s3, len); // Mutable borrow let mut s4 = String::from("hello"); append_world(&mut s4); println!("{}", s4); // "hello world" } fn calculate_length(s: &String) -> usize { s.len() // s goes out of scope, but it's just a reference, // so the value it refers to is not dropped } fn append_world(s: &mut String) { s.push_str(" world"); }
// Struct struct Person { name: String, age: u32, } // Method implementation for Person impl Person { fn new(name: String, age: u32) -> Person { Person { name, age } } fn greet(&self) { println!("Hi, I'm {} and I'm {} years old", self.name, self.age); } } // Enum enum Result{ Ok(T), Err(E), } fn main() { // Using structs let person = Person::new(String::from("Alice"), 30); person.greet(); // Pattern matching with Option let some_number = Some(5); let absent_number: Option<i32> = None; match some_number { Some(n) => println!("The number is {}", n), None => println!("No number found"), } // If let shorthand if let Some(n) = some_number { println!("Found number: {}", n); } }
Rust excels in many domains:
Rust has evolved through several phases:
Rust follows a 6-week release cycle for incremental updates and introduces major "editions" every three years that can include more significant syntax and feature changes while maintaining backward compatibility.
Here are some excellent resources for learning Rust:
Technologies and languages that relate to Rust: