How do I make this work
struct A<T, F: FnMut(T) -> T> {
f: F,
t: T,
}
fn foo<T, F: FnMut(T) -> T>() -> A<T, F> {
A {
t: 1,
f: |x|{x + 1},
}
}
How do I make this work
struct A<T, F: FnMut(T) -> T> {
f: F,
t: T,
}
fn foo<T, F: FnMut(T) -> T>() -> A<T, F> {
A {
t: 1,
f: |x|{x + 1},
}
}
Generics let the caller control the type. You can't use generics like that. You need to specify the return type of the function explicitly:
struct A<T, F: FnMut(T) -> T> {
f: F,
t: T,
}
fn foo() -> A<i32, impl FnMut(i32) -> i32> {
A {
t: 1,
f: |x| x + 1,
}
}
fn main() {
let mut a = foo();
println!("{:?}", (a.f)(3));
}