Reputation: 53
So I have several functions in a struct which return a value:
fn a(&mut self) -> u8{
//Do stuff
return 0;
}
fn b(z: u8) -> u8{return z;}
fn c(&mut self) -> u8{
//Do stuff
return 2;
}
I want to know if there is a way to call a function from an array which can be accessed by other member for functions, so, for example:
static FUNCTIONS: [u8; 3] = [a(), b(some_value), c()];
// OR
let functions: [u8; 3] = [a(), b(some_value), c()];
So in summation, I would like to do something like this:
struct this_struct {
value:u8
}
impl this_struct {
// The array of functions could be placed here for member access
fn a(&mut self) -> u8{
//Do stuff with value
return 0;
}
fn b(z: u8) -> u8{return z;}
fn c(&mut self) -> u8{
//Do stuff with value
return 2;
}
}
Basically, I just want to know if there is a way to call a function of a struct from an array in a way which will modify the struct's member variable. Is this possible? If so, how and where should I place the array?
Upvotes: 0
Views: 292
Reputation: 161
What you are trying to do here is not an array of functions but an array of functions results. Then you must specify from what struct you use functions ex.
static FUNCTIONS: [u8; 3] = [this_struct::a(), this_struct::b(value), this_struct::c()];
Additionaly in statics you can only use constant functions
impl this_struct {
const fn a() -> u8{return 0;}
const fn b(z: u8) -> u8{return z;}
const fn c() -> u8{return 2;}
}
Upvotes: 1