pub struct A {}
pub trait FooTakesFnMut {
fn foo<VF>(&self, mut fnmuti32: VF)
where
VF: FnMut(i32);
}
impl FooTakesFnMut for A {
fn foo<VF>(&self, mut fnmuti32: VF)
where
VF: FnMut(i32),
{
for x in 1..5 {
fnmuti32(x);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_00() {
let a = A;
let mut v = Vec::new();
let bar = |x| { v.push(x); };
a.foo(bar);
assert_eq!(v, vec![1,2,3,4]);
}
}
link to play rust playground copy
Compiler complains:
error: patterns aren't allowed in functions without bodies
--> src/lib.rs:9:23
|
9 | fn foo<VF>(&self, mut fnmuti32: VF)
| ^^^^^^^^^^^^ help: remove `mut` from the parameter: `fnmuti32`
<--cut-->
but after removing mut
from foo
functions signatures , it suggests adding them :
Compiling playground v0.0.1 (/playground)
error[E0596]: cannot borrow `fnmuti32` as mutable, as it is not declared as mutable
--> src/lib.rs:20:13
|
15 | fn foo<VF>(&self, fnmuti32: VF)
| -------- help: consider changing this to be mutable: `mut fnmuti32`
...
20 | fnmuti32(x);
| ^^^^^^^^ cannot borrow as mutable
<--cut-->