Vector of functions in rust

I am currently learning Rust and I am struggling with lifetime by creating a simple observer that will store a callback of an arbitrary type.

I started with a basic structure

struct Signal<T> {
    slots: Vec<|T|>
}

      

which gave me the initial lifetime error

signal_test.rs:7:16: 7:19 error: explicit lifetime bound required
signal_test.rs:7     slots: Vec<|T|>
                            ^~~
error: aborting due to previous error

      

So I'll try to add some lifetime specifications.

struct Signal<'r, T> {
    slots: Vec<'r |T|>
}

      

which is causing me some new errors

signal_test.rs:7:12: 7:23 error: wrong number of lifetime parameters: expected 0, found 1 [E0107]
signal_test.rs:7     slots: Vec<'r |T|>
                        ^~~~~~~~~~~
signal_test.rs:7:19: 7:22 error: explicit lifetime bound required
signal_test.rs:7     slots: Vec<'r |T|>

      

I haven't been able to find enough documentation for my entire life to hint what I need to do to fix this. This may not be a very good example to use in Rust. Some help and comments would be appreciated.

+3


source to share


1 answer


Try using this:



struct Signal<'r, T> {
    slots: Vec<|T|: 'r>
}

      

+3


source







All Articles