Struct including closure whose parameter is not Clone prevents the struct from being Clone

⚓ Rust    📅 2026-01-28    👤 surdeus    👁️ 1      

surdeus

I feel like my struct Bacon should be Clone. The fact that an argument to a closure is not Clone should not prevent the Rc from being Clone.

    use std::rc::Rc;

    #[derive(Clone)]
    struct Bacon<C> {
        callback: Rc<dyn Fn(C) -> f32>,
    }

    pub struct Meep {
        xy: [f32; 2],
    }

    pub fn doot() {
        let a: Bacon<&mut Meep> = Bacon {
            callback: Rc::new(|q| {
                q.xy[0] += 1.0;
                q.xy[1]
            }),
        };
        let b = a.clone();
    }

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0599]: the method `clone` exists for struct `Bacon<&mut Meep>`, but its trait bounds were not satisfied
  --> src/lib.rs:19:19
   |
 4 |     struct Bacon<C> {
   |     --------------- method `clone` not found for this struct because it doesn't satisfy `Bacon<&mut Meep>: Clone`
...
19 |         let b = a.clone();
   |                   ^^^^^ method cannot be called on `Bacon<&mut Meep>` due to unsatisfied trait bounds
   |
note: trait bound `&mut Meep: Clone` was not satisfied
  --> src/lib.rs:3:14
   |
 3 |     #[derive(Clone)]
   |              ^^^^^ unsatisfied trait bound introduced in this `derive` macro
   = help: items from traits can only be used if the trait is implemented and in scope
   = note: the following trait defines an item `clone`, perhaps you need to implement it:
           candidate #1: `Clone`

For more information about this error, try `rustc --explain E0599`.
error: could not compile `playground` (lib) due to 1 previous error

2 posts - 2 participants

Read full topic

🏷️ Rust_feed