Struct Vec>>

⚓ Rust    📅 2026-02-21    👤 surdeus    👁️ 1      

surdeus

Info

This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Struct Vec>>

Hello,

I'm facing a compiler error when pushing an Fn (in my case) in a vector of Box<Box<dyn Fn>>.

My function is this one:

pub fn on_property_change<C>(&'static mut self, callback: C)
    where
        C: Fn(&str, &DBusValue) + 'static,

And when I write this:

let callback_double_box = Box::new(Box::new(callback));
self.callbacks.push(callback_double_box);

I get this error:

 155 |     pub fn on_property_change<C>(&'static mut self, callback: C)
     |                               - found this type parameter
...
 166 |         self.callbacks.push(callback_double_box);
     |                        ---- ^^^^^^^^^^^^^^^^^^^ expected `Box<Box<dyn Fn(&str, &DBusValue)>>`, found `Box<Box<C>>`
     |                        |
     |                        arguments to this method are incorrect
     |
     = note: expected struct `Box<Box<(dyn for<'a, 'b> Fn(&'a str, &'b DBusValue) + 'static)>>`
                found struct `Box<Box<C>>`
     = help: type parameters must be constrained to match other types
     = note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters

Here is my struct:

struct MyStruct {
    callbacks: Vec<Box<Box<dyn Fn(&str, &DBusValue) + 'static>>>
}

I don't understand how C (in the case of my function) type is different from the type in my structure.

Thank you very much in advance for any help.

3 posts - 3 participants

Read full topic

🏷️ Rust_feed