Is it possible to write a const function that folds over an iterator? When I try:
const fn foo(s: &str) -> u64 {
return s.chars().fold(0, |accumulator, char| -> u64 {
return accumulator ^ (char as u64);
});
}
I get a compiler error:
error: function pointers in const fn are unstable
--> src/lib.rs:2:30
|
2 | return s.chars().fold(0, |accumulator, char| -> u64 {
| ______________________________^
3 | | return accumulator ^ (char as u64);
4 | | });
| |_____^
I presume that my anonymous function |x, y| -> x { ... }
is passed as a function pointer to fold()
and that's what's causing the error.
Is there some kind of const lambda that I can pass to fold
here, or can I just use a for loop instead and accumulate the result in a mutable variable which I then return from the foo
function? I have absolutely no Rust experience...