rust-ownership
Rust Ownership System
Master Rust's ownership system for writing safe, efficient, and concurrent code without garbage collection.
Core Concepts
Ownership Rules
- Each value has an owner
- Only one owner at a time
- Value dropped when owner goes out of scope
fn main() {
let s1 = String::from("hello");
let s2 = s1; // s1 moved to s2, s1 no longer valid
println!("{}", s2); // OK
// println!("{}", s1); // Error: value borrowed after move
}
Borrowing
fn main() {
let s1 = String::from("hello");
let len = calculate_length(&s1); // Borrow s1
println!("Length of '{}' is {}", s1, len); // s1 still valid
}
fn calculate_length(s: &String) -> usize {
s.len()
} // s goes out of scope but nothing happens (doesn't own the data)
Mutable References
fn main() {
let mut s = String::from("hello");
change(&mut s);
println!("{}", s); // "hello, world"
}
fn change(s: &mut String) {
s.push_str(", world");
}
Lifetimes
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() {
x
} else {
y
}
}
Best Practices
- Prefer borrowing over ownership transfer
- Use mutable references sparingly
- Understand lifetime elision rules
- Use smart pointers (Box, Rc, Arc) when needed
- Leverage the borrow checker
Resources
More from spjoshis/claude-code-plugins
excel-analysis
Master Excel for data analysis with pivot tables, formulas, Power Query, and advanced Excel techniques.
50flutter-performance
Optimize Flutter app performance with widget rebuilds, memory management, rendering optimization, and profiling techniques. Achieve smooth 60fps rendering.
10bloc-pattern
Master BLoC (Business Logic Component) pattern for Flutter with flutter_bloc. Learn events, states, testing, and advanced patterns for scalable apps.
9product-backlog-management
Master product backlog management with prioritization frameworks, refinement techniques, estimation, and continuous backlog optimization for maximum value delivery.
6laravel-development
Master Laravel 11 with Eloquent ORM, routing, middleware, queues, testing, and modern PHP development patterns.
6rxjs-patterns
Master RxJS in Angular with observables, operators, subjects, error handling, and reactive patterns for building responsive applications.
5