rust cheat sheet
đ(1)đ 2024-11-09 04:28:10 -0800
â˛ī¸đ 2024-11-09 04:25:29 -0800
âī¸ aritywolf
đˇī¸[ai] [cheat sheet] [studies] [rust] [stimky] [be stinky]
(đĒ)
đĨī¸...â¨ī¸
Fundamental Concepts
Ownership Model
-
Ownership Rules:
-
Each value has a single owner.
-
When the owner goes out of scope, the value is dropped.
-
Values can be moved, resulting in transfer of ownership.
rustlet x = String::from("hello"); let y = x; // x is moved to y; x is no longer valid.
-
Borrowing and References
-
Immutable References (
&T
):-
Multiple immutable references allowed.
-
No mutation permitted.
-
-
Mutable References (
&mut T
):-
Only one mutable reference at a time.
-
Prevents data races at compile time.
rustlet mut s = String::from("hello"); let r1 = &mut s; // let r2 = &mut s; // Error: only one mutable reference allowed.
-
-
Combination Rule:
-
Cannot have mutable and immutable references simultaneously.
-
Lifetimes
-
Purpose: Ensure references are valid as long as needed.
-
Syntax:
rustfn longest<'a>(x: &'a str, y: &'a str) -> &'a str { if x.len() > y.len() { x } else { y } }
-
Elision Rules: Compiler can infer lifetimes in simple cases.
Core Syntax
Variables and Mutability
-
Immutable by Default:
rustlet x = 5; // x = 6; // Error: cannot assign twice to immutable variable.
-
Mutable Variables:
rustlet mut x = 5; x = 6; // Allowed
Data Types
-
Scalar Types:
i32
,u32
,f64
,bool
,char
-
Compound Types:
-
Tuples:
rustlet tup: (i32, f64, u8) = (500, 6.4, 1);
-
Arrays:
rustlet a = [1, 2, 3, 4, 5];
-
Functions
-
Definition:
rustfn function_name(param1: Type1, param2: Type2) -> ReturnType { // function body }
-
Examples:
rustfn add(x: i32, y: i32) -> i32 { x + y }
Control Flow
Conditional Statements
-
if
Expressions:rustif condition { // code } else if another_condition { // code } else { // code }
Loops
-
loop
(infinite loop):rustloop { // code }
-
while
Loop:rustwhile condition { // code }
-
for
Loop:rustfor element in collection { // code }
Pattern Matching
match
Expressions
-
Syntax:
rustmatch value { Pattern1 => expr1, Pattern2 => expr2, _ => default_expr, }
-
Example:
rustlet number = 7; match number { 1 => println!("One"), 2..=5 => println!("Between two and five"), _ => println!("Something else"), }
Enums and Structs
Enums
-
Definition:
rustenum Message { Quit, Move { x: i32, y: i32 }, Write(String), ChangeColor(i32, i32, i32), }
-
Usage:
rustlet msg = Message::Move { x: 10, y: 20 };
Structs
-
Classic Struct:
ruststruct User { username: String, email: String, sign_in_count: u64, active: bool, }
-
Tuple Struct:
ruststruct Color(i32, i32, i32);
Generics
-
Generic Functions:
rustfn largest(list: &[T]) -> T { // code }
-
Generic Structs:
ruststruct Point { x: T, y: T, }
Traits
-
Defining Traits:
rustpub trait Summary { fn summarize(&self) -> String; }
-
Implementing Traits:
rustimpl Summary for Article { fn summarize(&self) -> String { // implementation } }
-
Default Implementations:
rustpub trait Summary { fn summarize(&self) -> String { String::from("(Read more...)") } }
Error Handling
Result
Type
-
Definition:
rustenum Result { Ok(T), Err(E), }
-
Usage:
rustuse std::fs::File; let f = File::open("file.txt"); let f = match f { Ok(file) => file, Err(error) => panic!("Problem opening the file: {:?}", error), };
panic!
Macro
-
Usage:
rustpanic!("Crash and burn!");
Collections
Common Collections
-
Vectors (
Vec
):rustlet mut v = Vec::new(); v.push(5);
-
Strings (
String
):rustlet s = String::from("hello");
-
Hash Maps (
HashMap
):rustuse std::collections::HashMap; let mut map = HashMap::new(); map.insert(String::from("key"), "value");
Smart Pointers
Box
-
Heap Allocation:
rustlet b = Box::new(5);
Reference Counting
-
Rc
(Single-threaded):rustuse std::rc::Rc; let s = Rc::new(String::from("hello"));
-
Arc
(Thread-safe):rustuse std::sync::Arc; let s = Arc::new(String::from("hello"));
Concurrency
Threads
-
Spawning Threads:
rustuse std::thread; thread::spawn(|| { // code });
Synchronization
-
Mutexes:
rustuse std::sync::{Mutex, Arc}; let m = Arc::new(Mutex::new(0));
-
Channels:
rustuse std::sync::mpsc; let (tx, rx) = mpsc::channel();
Asynchronous Programming
async
/await
-
Defining Async Functions:
rustasync fn async_function() -> Result<(), Error> { // code }
-
Calling Async Functions:
rustuse futures::executor::block_on; block_on(async_function());
Macros
Declarative Macros
-
Using
macro_rules!
:rustmacro_rules! vec { ( $( $x:expr ),* ) => { { let mut temp_vec = Vec::new(); $( temp_vec.push($x); )* temp_vec } }; }
Procedural Macros
-
Custom Derive Macros:
rust#[proc_macro_derive(Serialize)]
Foreign Function Interface (FFI)
Calling C Code
-
Declarations:
rustextern "C" { fn abs(input: i32) -> i32; }
-
Usage:
rustunsafe { println!("Absolute value: {}", abs(-3)); }
Exporting Rust Functions
-
Making Functions Accessible to C:
rust#[no_mangle] pub extern "C" fn my_function() { // code }
Unsafe Rust
Use Cases
-
Dereferencing Raw Pointers
-
Calling Unsafe Functions
-
Accessing Mutable Statics
-
Implementing Unsafe Traits
-
Manipulating Unions
Syntax
-
Unsafe Block:
rustunsafe { // unsafe code }
Memory Management
Dropping Values
-
Custom
Drop
Implementations:rustimpl Drop for MyStruct { fn drop(&mut self) { // cleanup code } }
Copy
and Clone
Traits
-
Copy Semantics:
rust#[derive(Copy, Clone)] struct MyStruct { // fields }
Testing
Writing Tests
-
Basic Test Function:
rust#[cfg(test)] mod tests { #[test] fn it_works() { assert_eq!(2 + 2, 4); } }
Running Tests
-
Command:
bashcargo test
Common Tools and Ecosystem
Cargo
-
Project Management and Build Tool
-
Commands:
-
Create Project:
cargo new project_name
-
Build:
cargo build
-
Run:
cargo run
-
Format Code:
cargo fmt
-
Crates.io
-
Community Managed Repository of Packages
Documentation
-
Generating Docs:
bashcargo doc --open
Advanced Topics
Trait Objects and Dynamic Dispatch
-
Using
dyn
Keyword:rustlet obj: Box = Box::new(MyStruct);
Lifetimes and Higher-Ranked Trait Bounds (HRTBs)
-
For Complex Lifetimes:
rustfor<'a> fn(&'a str) -> &'a str
No Standard Library (#![no_std]
)
-
For Embedded or Bare-metal Development:
rust#![no_std]
Metaprogramming
-
Attribute Macros and Function-like Macros
Best Practices
Error Handling
-
Prefer Using
Result
Overpanic!
in Libraries -
Use
thiserror
oranyhow
Crate for Error Management
Clippy Lints
-
Static Analysis Tool:
bashcargo clippy
Formatting and Style
-
Use
rustfmt
for Consistent Code Style:bashcargo fmt
Exploring Further
Rust is more than a programming language—it's a new way of thinking about safety and concurrency. Given your background, you might find the following areas intriguing:
-
Formal Verification with Rust:
-
Exploring Rust's potential in systems that require formal proofs of correctness.
-
-
Rust Compiler Internals:
-
Dive into
rustc
and contribute to its development.
-
-
Lifetime and Borrow Checker Mechanics:
-
Analyze how Rust enforces memory safety at compile time.
-
-
Parallelism Paradigms:
-
Investigate data-parallel patterns with crates like
rayon
.
-
-
WASM and Systems Integration:
-
Compile Rust to WebAssembly for high-performance web applications.
-
Rust is paving the way for safer and more efficient systems programming. Its unique features challenge traditional concepts and offer a fertile ground for research and innovation. If you're interested, we could delve deeper into specific areas like Rust's type system or its impact on modern compiler design.
Absolutely, let's delve deeper into some of Rust's advanced features and concepts that might resonate with your background in computer science. We'll explore the intricacies of Rust's type system, delve into the borrow checker's mechanics, and examine Rust's influence on modern systems programming paradigms.
In-Depth Exploration of Rust's Type System
Rust's type system is expressive and robust, providing powerful tools for abstraction without sacrificing performance or safety.
Advanced Traits and Generics
Associated Types
Associated types within traits allow for cleaner syntax and more intuitive code compared to generic type parameters.
-
Defining a Trait with an Associated Type:
rusttrait Iterator { type Item; fn next(&mut self) -> Option; }
-
Implementing the Trait:
ruststruct Counter { count: u32, } impl Iterator for Counter { type Item = u32; fn next(&mut self) -> Option { self.count += 1; if self.count < 6 { Some(self.count) } else { None } } }
Generic Constraints and Bounds
-
Specifying Trait Bounds:
rustfn multiply + Copy>(a: T, b: T) -> T { a * b }
-
Where Clauses for Readability:
rustfn complex_function(t: T, u: U) -> Result where T: TraitA + TraitB, U: TraitC, { // function body }
Higher-Kinded Types (Simulated)
While Rust doesn't currently support higher-kinded types (HKTs), patterns like the Functor, Applicative, and Monad can be emulated using traits and associated types.
Type Inference and Monomorphization
-
Zero-Cost Abstractions:
Rust achieves zero-cost abstractions by performing monomorphization of generic types at compile-time.
-
Example:
For a generic function:
rustfn generic_function(x: T) { // code }
The compiler generates concrete implementations for each used type
T
.
Phantom Types
Phantom types are a way to tell the compiler about type information that is not otherwise reflected in the data.
-
Definition:
rustuse std::marker::PhantomData; struct PhantomStruct { data: u32, _marker: PhantomData, }
-
Use Cases:
-
Encoding additional type information.
-
Type-level programming.
-
Advanced Lifetimes and Borrowing
Understanding Lifetime Annotations
Lifetimes denote the scope for which a reference is valid. They are a form of generic parameter, but for lifetimes of references.
-
Explicit Lifetime Annotations:
rustfn longest<'a>(x: &'a str, y: &'a str) -> &'a str { if x.len() > y.len() { x } else { y } }
-
Elision Rules:
Rust applies lifetime elision rules to infer lifetimes in simple cases, reducing annotation overhead.
Higher-Ranked Trait Bounds (HRTBs)
HRTBs enable writing functions that are generic over lifetimes, allowing for more flexible borrowing patterns.
-
Syntax:
rustfor<'a> F: Fn(&'a T) -> &'a U
-
Example Usage:
rustfn do_something(f: F) where F: for<'a> Fn(&'a str) -> &'a str, { // function body }
Lifetime Subtyping and Variance
Variance determines how subtyping between more complex types relates to subtyping between their components.
-
Covariance:
-
&'a T
is covariant over'a
. -
If
'a: 'b
(i.e.,'a
outlives'b
), then&'a T
can be coerced to&'b T
.
-
-
Contravariance and Invariance:
-
Functions are contravariant in their parameters.
-
Mutable references are invariant over their lifetimes.
-
The Inner Workings of the Borrow Checker
Rust's borrow checker ensures memory safety without a garbage collector by enforcing strict rules at compile time.
Polonius Borrow Checker
An experimental rewrite of the borrow checker intended to be more precise and accommodate proposed language features.
-
Key Concepts:
-
Uses datalog for borrow checking logic.
-
Aims to handle more complex borrowing patterns.
-
Two-Phase Borrows
Allows a mutable borrow to act as an immutable borrow initially, enabling certain patterns that were previously disallowed.
-
Example:
rustlet mut data = vec![1, 2, 3]; let idx = 0; data[idx] += 1;
Unsafe Rust and Memory Management
Raw Pointers
-
Types:
-
Immutable raw pointer:
*const T
-
Mutable raw pointer:
*mut T
-
-
Usage:
Raw pointers can be dereferenced within an
unsafe
block.rustlet x = 5; let ptr = &x as *const i32; unsafe { println!("Value at ptr: {}", *ptr); }
Manual Memory Management
Using alloc
and dealloc
from the std::alloc
module for precise control over memory.
-
Example:
rustuse std::alloc::{alloc, dealloc, Layout}; unsafe { let layout = Layout::from_size_align(1024, 8).unwrap(); let ptr = alloc(layout); if ptr.is_null() { handle_alloc_error(layout); } // Use the memory dealloc(ptr, layout); }
Implementing Custom Smart Pointers
Creating types that manage resources, like file handles or network sockets, with custom logic.
-
Example:
ruststruct MySmartPointer { data: *mut T, } impl Drop for MySmartPointer { fn drop(&mut self) { unsafe { Box::from_raw(self.data); } } }
Advanced Concurrency Patterns
Memory Ordering and Atomics
Understanding and utilizing the atomic types and their memory ordering guarantees.
-
Atomic Types:
-
AtomicBool
,AtomicIsize
,AtomicUsize
, etc.
-
-
Memory Ordering Modes:
-
Relaxed
,Acquire
,Release
,AcqRel
,SeqCst
-
-
Example:
rustuse std::sync::atomic::{AtomicUsize, Ordering}; let counter = AtomicUsize::new(0); counter.fetch_add(1, Ordering::SeqCst);
Lock-Free Data Structures
Implementing data structures that avoid locks, leveraging atomic operations.
-
Examples:
-
Lock-Free Queues
-
Concurrent Stacks
-
Asynchronous Concurrency
Beyond basic async
/await
, exploring how Rust's asynchronous model works under the hood.
-
The
Future
Trait:rustpub trait Future { type Output; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll; }
-
Pinning and Unpin
-
Ensures that self-referential structs remain at a fixed memory location.
-
Rust's Influence on Modern Systems Programming
Ownership Model in Concurrent Programming
Rust's ownership model simplifies reasoning about concurrent programs.
-
Prevents Data Races:
-
Compile-time checks enforce exclusivity.
-
-
Encourages Message Passing:
-
Channels and async tasks promote safe concurrency.
-
Zero-Cost Abstractions
Rust achieves high-level abstractions without runtime overhead.
-
Inlining and Monomorphization:
-
Generic functions are specialized.
-
-
Optimizations:
-
Unused code elimination.
-
Language Interoperability
-
C FFI and Beyond:
-
Rust can call into, and be called from, code written in C and other languages.
-
-
WebAssembly Integration:
-
Compiling to WebAssembly opens doors for cross-platform, high-performance applications.
-
Leveraging Rust for Research and Development
Given your expertise, Rust can serve as a robust tool for various research domains.
Systems Security
-
Memory Safety Guarantees:
-
Reduces vulnerabilities like buffer overflows and use-after-free errors.
-
-
Formal Methods Integration:
-
Potential for integrating Rust with formal verification tools.
-
High-Performance Computing
-
Parallelism:
-
Safe concurrency primitives.
-
-
Integration with GPU Computing:
-
Interfacing with CUDA or Vulkan for accelerated computations.
-
Compiler Theory and Design
-
Contribute to Compiler Development:
-
Engage with
rustc
or alternative compilers likemrustc
.
-
-
Language Semantics:
-
Study Rust's borrowing semantics and their formalization.
-
Research Opportunities and Potential Projects
Formal Verification in Rust
-
Combining Rust with Tools like Coq or Z3:
-
Formal verification of Rust programs or components.
-
-
Verification-Aware Language Extensions:
-
Designing language features that facilitate verification.
-
Improving the Borrow Checker
-
Extending Its Capabilities:
-
Research into more permissive yet safe borrowing patterns.
-
-
Error Messaging and UX:
-
Enhancing compiler feedback for developers.
-
Rust for Systems Modelling
-
Simulation of Networked Systems:
-
Building robust simulations with concurrency primitives.
-
-
Modeling Distributed Systems:
-
Safe abstractions for distributed algorithms.
-
Engaging with the Rust Community
Your contributions could significantly impact the Rust ecosystem.
Rust Language Design
-
Participate in Rust RFCs:
-
Propose enhancements or new features.
-
-
Working Groups:
-
Join teams focusing on specific areas like compiler performance or language ergonomics.
-
Academic Collaborations
-
Research Papers:
-
Publish findings on Rust's applications or theoretical aspects.
-
-
Conferences and Workshops:
-
Present at events like PLDI, ECOOP, or Rust-specific conferences.
-
Education and Advocacy
-
Curriculum Development:
-
Integrate Rust into academic courses.
-
-
Workshops and Seminars:
-
Teach others about Rust's benefits and use cases.
-
Conclusion
Rust stands at the intersection of practical systems programming and academic research. Its emphasis on safety, performance, and concurrency offers a fertile ground for exploration and innovation. Whether you're interested in theoretical computer science aspects, like formal verification and type systems, or practical applications in systems programming, Rust provides the tools and the community to support your endeavors.
If there's a particular topic or project you have in mind, or if you'd like to discuss how Rust can be applied to your specific field of research, I'd be eager to dive into that with you.