I got the error messages here:
error[E0277]: the trait bound `impl std::ops::FnOnce<()>: std::ops::FnOnce<(_,)>` is not satisfied
--> src\lib.rs:43:39
|
43 | accumulate(tuples, i).unwrap_or_else(compose(Cow::<str>::from, apply_0(ToString::to_string, &i)))
| ^^^^^^^ trait `impl std::ops::FnOnce<()>: std::ops::FnOnce<(_,)>` not satisfied
|
= note: required by `compose`
error[E0277]: the trait bound `impl std::ops::FnOnce<(_,)>: std::ops::FnOnce<()>` is not satisfied
--> src\lib.rs:43:24
|
43 | accumulate(tuples, i).unwrap_or_else(compose(Cow::<str>::from, apply_0(ToString::to_string, &i)))
| ^^^^^^^^^^^^^^ trait `impl std::ops::FnOnce<(_,)>: std::ops::FnOnce<()>` not satisfied
Maybe experienced Rust devs know exactly how to diagnose the issue, but the error message to me is useless. It just says the trait is not satisfied. Not satisfied by what? Who knows?
fn apply_0<F, V, R>(f: F, v: V) -> impl FnOnce()->R
where
F: FnOnce(V)->R
{
|| f(v)
}
pub fn compose<A, B, C, F, G>(f: F, g: G) -> impl FnOnce(A) -> C
where G: FnOnce(A) -> B,
F: FnOnce(B) -> C,
{
move |a: A| { f(g(a)) }
}
This is where you hit the REAL learning curve. Everything seems like it’s FnOnce and it doesn’t compile.
This isn’t even the first time, every time I try to do functions that return other functions, it gets very confusing in Rust.
The problem is that compose has the wrong function type
it should be
fn compose<A, B, F, G>(f: F, g: G) -> impl FnOnce() -> B
where G: FnOnce() -> A, F: FnOnce(A) -> B {
|| f(g())
}
because that function doesn’t take an argument, but the error message is very cryptic to beginners. In my experience, “my program doesn’t work and I don’t know how to fix it” has been a much bigger sink of time for me than “my program doesn’t work and I can throw asterisks and refs at it until it does”.