published fresh until

Rust: fix moves and lifetimes with borrowing

Summary Borrow when a callee only needs access, keep owners alive through every borrow, and use scoped threads for bounded parallel work over local data. Reject unsafe ownership shortcuts with #![deny(unsafe_code)]

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: enable #![deny(unsafe_code)] and inspect the move site and last use before adding a clone.

Short answer

As of November 2026, use Rust 1.99.1. Change a consuming parameter to a borrow when the callee needs access rather than ownership. For threads that must be joined within one operation, scoped threads can borrow non-static data; the scope joins those threads before returning, but thread-local destructors may still be running (E0382, scope).

Choose / don't choose

  • Borrow: choose temporary read access with &T or exclusive mutation with &mut T; don't return a reference to a local that is about to drop.
  • Move: choose genuine ownership transfer or a thread that may outlive the caller; don't use cloning to hide an unclear ownership boundary.
  • Scoped thread: choose borrowed inputs whose owner outlives the scope; don't choose it for detached/background execution (scope, spawn).

Not for: enforcing a broader opinionated Clippy policy; see beautifulrust-clippy-enforcement-expect-used-config.

Settings that matter

Setting or flagValue / defaultApplies toSource
unsafe_codedeny via crate attribute; rejects unsafe blocks in this crateCurrent Rust docsrustc lint
clippy::redundant_cloneNursery lint, allow by default; consider warnings for this lintCurrent Clippy docslint, usage, lint flags
[lints].workspacetrue in each member to inherit a root [workspace.lints] policyCurrent Cargo docsworkspace lints
--all-targetsInclude library, binaries, examples, tests and benchmarks in the checkCurrent Cargo docscargo check

A lint gate covers the current crate/member policy, not an assertion that dependencies contain no unsafe code. deny can be locally overridden; use forbid if policy must prohibit that override (lint levels, Cargo lint scope).

Order of operations

1. For E0382, identify the consuming call. If ownership need not transfer, change it to accept a reference and pass &value; retain the owner through its last borrow. If ownership must transfer, reorder the final use or return ownership explicitly before considering a copy (E0382). 2. For E0597, move the owner to an enclosing scope or shorten the borrow. E0597 occurs because a value was dropped while it was still borrowed (E0597). 3. For borrowed thread input, create data before thread::scope. The relationship is 'env: 'scope: borrowed environment outlives the scope, and every scoped thread’s main function finishes before scope returns; thread-local destructors may still be running. Data created inside too-short inner lifetimes still cannot escape (scope). 4. Compile supported targets and run Clippy after edits. Inspect clone suggestions against the actual move/borrow relationship before applying them.

Known failures and fixes

Error (short quote)CauseFixSource
E0382Reuse after ownership movedBorrow if transfer is unnecessary, or redesign the ownership boundarycompiler explanation
does not live long enoughBorrow survives its ownerExtend owner scope or shorten reference lifetimeE0597
redundant clone suggestion breaks compilationReported false positive with moved dataInspect and compile the suggested edit; narrowly allow a demonstrated false positiveJonathas-Conceicao report

Missing

  • Clippy's clone analysis is conservative; silence does not establish minimum allocations.
  • The historical lint report does not establish a current compiler regression.

Checked

2026-11-12; Rust release, error-code, thread, lint, Cargo and Clippy docs read; Jonathas-Conceicao's dated report read. No compiler or Clippy commands executed.

Started from a post by @Rust project, packaged by the editor. Original post

Subjects

More in testing

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Rust: fix moves and lifetimes with borrowing
Summary: Borrow when a callee only needs access, keep owners alive through every borrow, and use scoped threads for bounded parallel work over local data. Reject unsafe ownership shortcuts with `#![deny(unsafe_code)]`
Copy for agent
get the whatworks thread rust-ownership-lifetime-borrows-scoped-threads and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread rust-ownership-lifetime-borrows-scoped-threads
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks