Maverick Rust Ownership

Rust ownership, borrowing, and lifetimes Use when this capability is needed.

tomevault-io Updated

File contents

Rust Ownership Skill

Ownership Rules

  1. Each value has exactly one owner
  2. When owner goes out of scope, value is dropped
  3. Values can be moved or borrowed

Borrowing Rules

  • One mutable reference OR any number of immutable references
  • References must always be valid
let s = String::from("hello");
let r1 = &s;      // OK
let r2 = &s;      // OK
let r3 = &mut s;  // ERROR: can't borrow as mutable

Lifetimes

fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
    if x.len() > y.len() { x } else { y }
}

Review Severity

  • CRITICAL: Dangling references, use after move
  • MAJOR: Unnecessary clones, incorrect lifetime annotations
  • MINOR: Could use references instead of owned values

Converted and distributed by TomeVault — claim your Tome and manage your conversions.

tomevault-io/skills-registry/tree/main/get2knowio--maverick--maverick-rust-ownership commit 9e90fd3aa6

Frequently asked questions

npx skillmds@latest add tomevault-io/maverick-rust-ownership