Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vector of functions in Rust

Tags:

rust

I am currently learning Rust and I am struggling with lifetimes while creating a simple observer that would store a callback of an arbitrary type.

I started with a basic struct

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

which got 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 next I try adding some lifetime specifiers.

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

which gets 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 have not been able to find enough rust lifetime documentation to hint at what I need to do to fix this. It could just be this is not a good pattern to use in Rust. Some help and comments would be appreciated.

like image 404
Eric Y Avatar asked Sep 29 '22 12:09

Eric Y


1 Answers

Try using this:

struct Signal<'r, T> {
    slots: Vec<|T|: 'r>
}
like image 153
Anonymous Avatar answered Oct 07 '22 20:10

Anonymous