I would like to modify the value inside the function that call function several times internally, how can I achieve it?
It works fine if I run Function
once, (without Copy)
But, if I would like to run Function
several times, I need Copy
on Function
Then, errors occur,
note:
std::marker::Copy
is implemented for&u32
, but not for&mut u32
I have no idea how to go any further.
#![allow(unused)]
fn print_string<F>(f: F)
where
F: FnOnce() + Copy,
{
for i in 0..2 {
f();
}
// Wwork fine if we run F once
// f();
}
fn main() {
// some others value that initialize here
let mut call_func_time: u32 = 0;
print_string(|| {
// modify the value, this function will call several times, so we can't initialize the values here.
// and the value will only be modified by this function called.
call_func_time += 1;
println!("{}", format!("{}", call_func_time))
});
}