Can anybody help me figure out how to get a function to return a closure that captures the function's environment? This is my current attempt, but it won't compile.
fn main() {
dbg!(test_func("test".into())());
}
fn test_func<F: FnOnce() -> String>(arg: String) -> F
{
|| arg
}
Errors:
Compiling playground v0.0.1 (/playground)
error[E0282]: type annotations needed
--> src/main.rs:2:10
|
2 | dbg!(test_func("test".into())());
| ^^^^^^^^^ cannot infer type for `F`
|
= note: type must be known at this point
error[E0308]: mismatched types
--> src/main.rs:7:5
|
5 | fn test_func<F: FnOnce() -> String>(arg: String) -> F
| - expected `F` because of return type
6 | {
7 | || arg
| ^^^^^^ expected type parameter, found closure
|
= note: expected type `F`
found type `[closure@src/main.rs:7:5: 7:11 arg:_]`
= help: type parameters must be constrained to match other types
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0282, E0308.
For more information about an error, try `rustc --explain E0282`.
error: could not compile `playground`.
To learn more, run the command again with --verbose.