Skip to content

Suggest cloning Arc/Rc #104232

Open

Description

fn main() {
    let x = Arc::new(1);
    foo(x);
    bar(x);
}

fn foo(_: Arc<usize>) {}
fn bar(_: Arc<usize>) {}
error[[E0382]](https://doc.rust-lang.org/stable/error-index.html#E0382): use of moved value: `x`
 --> src/main.rs:6:9
  |
4 |     let x = Arc::new(1);
  |         - move occurs because `x` has type `std::sync::Arc<usize>`, which does not implement the `Copy` trait
5 |     foo(x);
  |         - value moved here
6 |     bar(x);
  |         ^ value used here after move

The help message could mention that Arc implements Clone. We don't usually suggest cloning values in diagnostics, but for Arc/Rc we probably should.

Another common case (this one is probably harder to suggest a fix for):

fn main() {
    let x = Arc::new(1);
    for _ in 0..4 {
        std::thread::spawn(move || {
            println!("{}", x);
        });
    }
}
error[[E0382]](https://doc.rust-lang.org/stable/error-index.html#E0382): use of moved value: `x`
 --> src/main.rs:6:28
  |
4 |     let x = Arc::new(1);
  |         - move occurs because `x` has type `std::sync::Arc<i32>`, which does not implement the `Copy` trait
5 |     for _ in 0..4 {
6 |         std::thread::spawn(move || {
  |                            ^^^^^^^ value moved into closure here, in previous iteration of loop
7 |             println!("{}", x);
  |                            - use occurs due to use in closure
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions