Use when Rust's ownership system including ownership rules, borrowing, lifetimes, and memory safety. Use when working with Rust memory management.
View on GitHubTheBushidoCollective/han
jutsu-rust
January 24, 2026
Select agents to install to:
npx add-skill https://github.com/TheBushidoCollective/han/blob/main/jutsu/jutsu-rust/skills/rust-ownership-system/SKILL.md -a claude-code --skill rust-ownership-systemInstallation paths:
.claude/skills/rust-ownership-system/# Rust Ownership System
Master Rust's unique ownership system that provides memory safety without
garbage collection through compile-time checks.
## Ownership Rules
**Three fundamental ownership rules:**
1. Each value in Rust has a variable that's its owner
2. There can only be one owner at a time
3. When the owner goes out of scope, the value is dropped
```rust
fn ownership_basics() {
// s owns the String
let s = String::from("hello");
// Ownership moved to s2
let s2 = s;
// Error: s no longer owns the value
// println!("{}", s);
println!("{}", s2); // OK
} // s2 dropped here, memory freed
```
## Move Semantics
**Ownership transfer (move):**
```rust
fn move_semantics() {
let s1 = String::from("hello");
// Ownership moved to function
takes_ownership(s1);
// Error: s1 no longer valid
// println!("{}", s1);
}
fn takes_ownership(s: String) {
println!("{}", s);
} // s dropped here
// Return ownership from function
fn gives_ownership() -> String {
String::from("hello")
}
fn main() {
let s = gives_ownership();
println!("{}", s);
}
```
**Copy trait for stack types:**
```rust
fn copy_types() {
// Types implementing Copy are duplicated, not moved
let x = 5;
let y = x; // x copied to y
println!("x: {}, y: {}", x, y); // Both valid
// Copy types: integers, floats, bool, char, tuples of Copy types
let tuple = (1, 2.5, true);
let tuple2 = tuple;
println!("{:?} {:?}", tuple, tuple2); // Both valid
}
```
## Borrowing
**Immutable borrowing (references):**
```rust
fn immutable_borrow() {
let s1 = String::from("hello");
// Borrow s1 (immutable reference)
let len = calculate_length(&s1);
println!("Length of '{}' is {}", s1, len); // s1 still valid
}
fn calculate_length(s: &String) -> usize {
s.len()
} // s goes out of scope, but doesn't drop the value
// Multiple immutable borrows allowed
fn multiple_immutable_borrows() {
let s = String::fr