rust-ownership-borrowing

Explain Rust ownership, borrowing, and lifetimes to prevent memory-safety errors.

Updated May 26, 2026
One-click install
npx skills add https://github.com/adelabdelgawad/rust-fullstack-agents --skill rust-ownership-borrowing
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: rust-ownership-borrowing
Source: https://github.com/adelabdelgawad/rust-fullstack-agents/tree/main/plugins/rusty/skills/rust-ownership-borrowing
Command: npx skills add https://github.com/adelabdelgawad/rust-fullstack-agents --skill rust-ownership-borrowing

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Clarify Rust's ownership, borrowing, and lifetimes to prevent memory-safety bugs and guide correct code patterns.

Core Features & Use Cases

  • Explains ownership transfer, borrowing rules (immutable vs mutable), and non-lexical lifetimes (NLL).
  • Demonstrates when to clone vs borrow and how to design safe APIs that avoid moving out of borrowed data.
  • Provides concrete examples for slices, &str, and common patterns that trigger borrow-checker errors (E0382, E0499, E0502, E0507, E0515).

Quick Start

Explain a borrow-checker error and show a safe alternative using borrow or clone.

Frequently Asked Questions about rust-ownership-borrowing

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
Why does the Rust borrow checker throw error E0382 when I try to use a variable after passing it to a function?

Rust error E0382 occurs because ownership is transferred when a variable is moved into a function. The original variable can no longer be used, but you can resolve this by borrowing the value instead of moving it.

How do I fix Rust borrow checker errors E0499 and E0502 when iterating over a collection?

Errors E0499 and E0502 happen when multiple mutable references or overlapping borrows exist. Fix them by ensuring only one mutable borrow is active at a time, or use immutable borrows and non-lexical lifetimes (NLL) to allow earlier scope resolution.

What is the difference between moving and borrowing in Rust, and when should I clone instead?

Moving transfers ownership entirely, while borrowing allows temporary access without transferring ownership. Clone data when you need a new owned copy to avoid borrow checker conflicts, especially when designing safe APIs that require multiple independent mutable states.

How do Rust lifetimes and non-lexical lifetimes (NLL) affect how long a borrow is valid?

Rust lifetimes define how long references remain valid, preventing dangling pointers. Non-lexical lifetimes (NLL) shorten a borrow's scope to its last actual use rather than its lexical block end, making borrowing rules more flexible and reducing compiler errors.

How do I prevent moving out of borrowed data when designing safe Rust APIs?

Prevent moving out of borrowed data by returning borrowed slices or using references like &str instead of owned types. Design APIs to borrow inputs immutably where possible, and clone only when the caller requires independent ownership of the data.

What does Rust error E0507 mean when I try to move a value out of a borrowed reference?

Rust error E0507 means you attempted to move a value out of a borrowed reference, which violates ownership rules. Resolve it by cloning the value, replacing it with a placeholder like Default, or restructuring the code to take ownership directly.