This question is following up from this SO question.
As a novice in Rust, I am trying to understand the way a function pointer (fn) is initialized. Here's an abridged and much simpler description of the application I am writing.
A Controller
is initialized with a route: i32
value. Later, this value may be modified and all the old values are stored in a list, as a history of changes.
Here's a sample 'lib.rs` content:
use futures::future;
use random_number::random;
pub struct Controller {
pub route: i32,
pub running_history_modified_routes: Vec<i32>
}
impl Controller {
// a 'route' is initialized with an integer
pub fn new(p: i32) -> Controller {
Controller { route: p, running_history_modified_routes: Vec::new()}
}
// Subsequently, the 'route' may get a new value.
// Here, we are generating a random new value and storing that in
// vector, as history of changes to route.
pub fn compute_a_new_route (&mut self, using_seed: i32) -> &mut Controller {
// My confusion is around this initialization!
let as_function_pointer: fn(i32) -> i32 = free_function_generate_random_route_value;
let a_fresh_route = self.get_a_route_afresh(using_seed,as_function_pointer);
self.running_history_modified_routes.push(a_fresh_route);
self
}
fn get_a_route_afresh(&self, seed_as_higher_limit: i32, f:fn(i32) -> i32) -> i32 {
f(seed_as_higher_limit)
}
fn method_generate_random_route_value(&self,seed_as_higher_limit: i32) -> i32 {
random!(0 as i32, seed_as_higher_limit)
}
fn assoc_function_generate_random_route_value(seed_as_higher_limit: i32) -> i32 {
random!(0 as i32, seed_as_higher_limit)
}
}
fn free_function_generate_random_route_value(seed_as_higher_limit: i32) -> i32 {
random!(0 as i32, seed_as_higher_limit)
}
fn get_a_route_afresh(..)
receives a function pointer and calls it to get the new route value (which is a random number, for this example, of course :-) ).
I have three different candidates for the function pointer (commented in the code above):
- Controller's implementation method
method_generate_random_route_value
- Controller's associated function
assoc_function_generate_random_route_value
- Module's free function
free_function_generate_random_route_value
My understanding is that each of these can be used for initializing a function pointer before calling self.get_a_route_afresh(using_seed,as_function_pointer)
, in the same way! But, the compiler disagrees when I do this:
let as_function_pointer: fn(i32) -> i32 = self.method_generate_random_route_value;
and tells me this:
error[E0615]: attempted to take value of method `method_generate_random_route_value` on type `&mut Controller`
--> src/lib.rs:20:60
|
20 | let as_function_pointer: fn(i32) -> i32 = self.method_generate_random_route_value;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ method, not a field
|
help: use parentheses to call the method
|
20 | let as_function_pointer: fn(i32) -> i32 = self.method_generate_random_route_value(_);
| +++
Of course, when I use the associated or free function, both the compiler and I, are happy.
What is it that I am missing about a (impl) method's applicability when a function pointer is expected?
Here's Cargo.toml
:
[package]
name = "FutureExperiment"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
random-number = "0.1.8"
[lib]
name="function_pointers_experiment"
path="src/lib.rs"