Basically I want to be able to store a function as a field in a struct, then call that function later. Fairly simple, but I can't figure out a good way to do it.
For example:
struct Car<'a> {
wheels: i32,
capacity: i32,
drive: Box<dyn Fn() -> &'a str>
}
fn drive_car() -> &str {
"vroom"
}
fn main {
let car = Car {
wheels: 4,
capacity: 5,
drive: drive_car()
}
car.drive()
}
So how can I go about making this happen. When I try to do this, I am the error message: no method named drive found for reference in the current scope
from car.drive()
.
Sorry if there are a bunch of mistakes in there, still trying to learn rust.