Joke's on you: it took me a month of asking around why my program won't compile to figure out I really CAN'T do it without dereferencing inside the function. This is because of higher ranked lifetime bounds... there's no way to express the correct lifetime bound in this case (in Rust, maybe in Haskell it's possible), so you just have to deref first.
> This is because of higher ranked lifetime bounds...
No, it's because the function you're returning is of type FnMut(&B) -> C, but your F function is of type FnMut(B) -> G. It expects an owned value of B, but you only have a borrowed reference to a B. Just make F of type FnMut(&B) -> G and you no longer have to dereference and no longer need the restriction that B: Copy. Higher-ranked lifetime values don't have anything to do with it.
You think I didn't try that one before? It doesn't work in MY case:
error[E0281]: type mismatch: the type `fn(_) -> _ {tool::second::<_>}` implements the trait `std::ops::FnMut<(_,)>`, but the trait `for<'r> std::ops::FnMut<(&'r _,)>` is required (expected concrete lifetime, found bound lifetime parameter
--> src\lib.rs:50:17
|
50 | .filter(apply(second, i))
| ^^^^^
|
= note: required by `apply`
error[E0271]: type mismatch resolving `for<'r> <fn(_) -> _ {tool::second::<_>} as std::ops::FnOnce<(&'r _,)>>::Output == _`
--> src\lib.rs:50:17
|
50 | .filter(apply(second, i))
| ^^^^^ expected bound lifetime parameter , found concrete lifetime
|
= note: concrete lifetime that was found is lifetime '_#11r
= note: required by `apply`
error: aborting due to 2 previous errors
error: Could not compile `fizzbuzz`.
fn main() {
for i in 1..101 {
if i % 15 == 0 {
println!("FizzBuzz");
} else if i % 3 == 0 {
println!("Fizz");
} else if i % 5 == 0 {
println!("Buzz");
} else {
println!("{}", i);
}
}
}
It doesn't do the same exact thing as mine does. For one thing, mine does FizzBuzzBazz (factors of 3, 5, 7) or even FizzBuzzBazzQuux if you want (actually arbitrary strings and arbitrary conditions)