/rewrite-rs-skills:ownership-not-clonefrom rewrite-rs-skills
Guides Rust developers to replace unnecessary clones, Rc, RefCell, or Arc<Mutex> with ownership and borrowing. Useful when silencing the borrow checker with clones or reviewing clone-heavy code.
Ships in rewrite-rs-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
The borrow checker is a map of where ownership is being fudged, not an obstacle to route around. This skill decides who holds a value and for how long.
Every clone must be explainable in one sentence that is not "the borrow checker
complained." A clone that buys a real thing — a value that must outlive the borrow
— is fine; one that only silences an error is a deferred design decision.
The decision is deliberate, not forbidden. When the borrowing alternative is
genuinely contorted — lifetimes threaded through five signatures to save a copy
of a few bytes — a clone is allowed, as a commented choice that weighs what the
copy costs against what the contortion costs. What it may not be is a reflex.
A clone added only to quiet the checker also creates a second value: edits made
to the copy never reach the original, so the program can quietly diverge from
what the author meant. In a prototype, clones are acceptable provided each is
still a deliberate, findable choice that gets revisited before the code ships.
When the alternative to borrowing is a type that owns its data, owning wins
more often than not: an owned Vec<u8> field spares every container the
lifetime parameter, and copying small data once is usually fine. Details on
lifetimes, pointer types, and borrow tactics are in LIFETIMES.md.
E0502 (a mutable borrow while an immutable one is still live) and E0499 (two
mutable borrows of the same place) name a lifetime conflict; the fix is in the
structure the error describes, not in the clone that silences it. A reference
lives until its last use rather than the end of its block, so moving the final
read of the shared borrow ahead of the mutation often ends the conflict:
// No clone: `first` is Copy, so the borrow ends at the let.
let first = items[0];
for _ in 0..n {
items.push(first);
}
The borrow checker tracks a whole value through method calls, but fields
individually through direct field access. A &mut self method that also needs
self.other_field is the classic false conflict — destructure once:
// The two fields are separate lets and borrow independently.
impl Server {
fn handle(&mut self) {
let Self { connections, log, .. } = self;
for conn in connections.iter_mut() {
log.record(conn.id());
}
}
}
split_at_mut hands back two disjoint &mut halves of one slice; when
destructuring is not enough, extract a free function that takes the two fields.
When one large struct keeps producing these conflicts, split the struct: smaller
types composed in the parent borrow separately, and the new seams are usually
a better design than the old blob.
&str over &String, &[T] over &Vec<T>, impl AsRef<Path> over a PathBuf
for a path a function only reads. A signature that takes String when it only
reads forces every caller into a clone — the bug is in the signature.
The same rule covers the wrappers: &T over &Box<T>, and Option<&T> over
&Option<T> (call as_ref() at the call site). A borrowed parameter accepts
owned values, borrows, and literals through deref coercion, and &String or
&Vec<T> adds a second pointer hop for nothing. Clippy's borrowed_box and
ptr_arg flag these.
The other direction matters just as much. When the function needs to own the
value — to store it, send it, or consume it — take it by value and let the caller
decide whether to clone or move. Never take &T and clone inside: the caller
who had an owned value to give is forced to pay a copy anyway. And bound a
generic by Copy only when the body requires it, not to announce that the
type is cheap.
// Forces a clone at every call site that holds a &str.
fn log_message(message: String) { ... }
// Reads only — callers pass a reference and copy nothing.
fn log_message(message: &str) { ... }
Cow for genuine borrow-or-ownCow<'_, str> earns its keep when the common path returns the input untouched
and the rare path allocates.
use std::borrow::Cow;
// The common path borrows; only input containing a bracket allocates.
fn sanitize(input: &str) -> Cow<'_, str> {
if input.contains(['<', '>']) {
Cow::Owned(input.replace('<', "<").replace('>', ">"))
} else {
Cow::Borrowed(input)
}
}
Rc/Arc/RefCell/Mutex express a real requirementShared ownership is justified when the runtime structure genuinely has multiple
owners with no single one outliving the rest, and not when a single owner is
merely inconvenient to name. Run the four-question test in SHARED-STATE.md
before adding a RefCell; the decision table is in CLONE-DECISIONS.md; the
handle clone that passes the one-sentence test is in SHARED-STATE.md.
mem::take and mem::replaceWhen the code holds only a &mut and needs an owned value, mem::take and
mem::replace swap something in so the original can be moved out:
let old = mem::take(&mut self.buffer);
take leaves Default::default() behind (an empty String or Vec does not
allocate); replace leaves a value you pick; Option::take does this for an
Option field. To change an enum variant in place, match on &mut, take the
field you want to keep, and assign the new variant around it.
use std::mem;
enum Job {
Queued { label: String, attempts: u32 },
Failed { label: String },
}
fn give_up(job: &mut Job) {
if let Job::Queued { label, attempts } = job {
if *attempts >= 3 {
// `label` is a &mut String; take it so no clone is needed.
*job = Job::Failed { label: mem::take(label) };
}
}
}
A static with mutable or lazily-initialised contents has no visible owner and
no stated lifetime. Hand the value in as an argument, or keep it in the type that
does the work.
Two concrete failure modes make this more than taste. A thread_local! gives
every thread a separate copy, so it breaks wherever one shared value is the
point. And if the dependency graph holds two semver-incompatible versions of a
crate, each version carries its own statics, so a registry or counter that looked
global is quietly two of them. Likewise avoid code that runs before main, such
as ctor-style crates: its ordering is invisible to the program. Start
everything explicitly from main.
Which pointer type holds the value (&, Box, Rc/Arc, RefCell, Mutex),
when a lifetime must be named, why owning fields beat borrowed ones in a struct,
what 'static really asks for, the order of tactics when a conflict survives, and
why self-referential structs are avoided are all in LIFETIMES.md.
Reshaping how correct code is expressed is /idiomatic-rust; modelling invariants
into types is /type-driven-design; error types are /rust-errors; shared state
across .await points is /async-rust.
Keeping or removing a clone changes no behaviour, so the claim "this still works" is settled by the suite, at the lint level the repo configures:
cargo clippy --all-targets # add -- -D warnings only if the repo has no lint config
cargo test
clippy::redundant_clone is nursery and off by default — run it one-off with
cargo clippy -- -W clippy::redundant_clone as a proposal generator, never write
it into the repo lint config; its findings are candidates, not edits. The
default-on clippy::borrowed_box and clippy::ptr_arg catch the over-narrow
argument types from the signature section; fix what they report.
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub rewrite-rs/skills --plugin rewrite-rs-skills28 skills in this plugin
/async-rustWrite correct async Rust — runtime choice, Send and Sync bounds, cancellation safety, blocking work inside async contexts, and shared state across tasks. Use when writing or reviewing async code, when a future is held across an await point, when the user hits a Send bound error on a spawned task, when a runtime stalls or deadlocks, or when the user asks about tokio, select!, or spawn_blocking./idiomatic-rustWrite Rust that reads like Rust — iterator pipelines over index loops, From/Into over ad-hoc converters, derives over hand-written impls, newtypes over bare primitives. Use when writing new Rust, when reviewing Rust that reads like a translation from another language, when reaching for a classic design pattern (strategy, command, visitor) or a try/finally cleanup in Rust, when naming a constructor, accessor, iterator, or error type, or when the user asks how to make Rust code more idiomatic or less repetitive./port-from-cPort C into Rust — recovering the ownership, lifetime, nullability, and length that C never recorded, mapping pointers to references and slices, tag-plus-union to enums, return codes and errno to Result, and linking Rust into the existing build with bindgen and cbindgen. Use when porting, rewriting, or migrating C, a C library, firmware, or a C application into Rust, when replacing C modules one at a time behind the existing build, or when the user asks how a C construct such as malloc, a char pointer, a union, or a preprocessor macro translates to Rust./port-from-cppPort C++ into Rust — RAII and smart pointers onto ownership, templates onto generics and traits, exceptions onto Result, the STL onto Rust collections, and the traps (move semantics, implicit conversions, undefined behaviour, iterator invalidation). Use when porting, rewriting, or migrating C++, a C++ library, or a C++ application into Rust, when replacing a C++ module with Rust behind the existing build, or when the user asks how a C++ construct such as unique_ptr, shared_ptr, a template, or an STL container translates to Rust./port-from-goPort Go into Rust — goroutines and channels onto tasks and async, interfaces onto traits, error values onto Result, and the value-semantics traps (zero values, integer wraparound, slice aliasing, nil). Use when porting, rewriting, or migrating Go, Golang, a Go service, or a Go CLI into Rust, when replacing a Go component with a Rust one behind the same interface, or when the user asks how a Go construct translates to Rust./port-from-javaPort Java into Rust — class hierarchies onto enums and composition, exceptions onto Result, collections and streams onto Rust equivalents, and the traps (UTF-16 strings, silent integer wraparound, null, equals/hashCode contracts). Use when porting, rewriting, or migrating Java, a JVM service, a Spring or Jakarta application, or a Java library into Rust, when replacing a JVM component with a Rust one behind the same interface, or when the user asks how a Java construct translates to Rust.Other plugins' skills for the same job