These are chat archives for rust-lang/rust

16th
Jun 2017
NicolasVidal
@NicolasVidal
Jun 16 2017 21:24
Hi everyone :)
I've got a compiler error, and don't understand how to fix it
fn test_functions_compiling(functions: &[&Fn(usize)-> usize; 4]) {
    for (i, f) in functions
        .iter()
        .enumerate() {
        println!("{}", f(i));
    }
}

fn test_functions_not_compiling(functions: &[&Fn(usize)-> usize; 4]) {
    functions
        .iter()
        .enumerate()
        .map(&|i, f| println!("{}", f(i)))
        .collect();
}
Ilya Bogdanov
@vitvakatu
Jun 16 2017 21:28
First of all, remove & in map
NicolasVidal
@NicolasVidal
Jun 16 2017 21:28
the error is the dreaded :
error: the type of this value must be known in this context
Ilya Bogdanov
@vitvakatu
Jun 16 2017 21:29
You need to specify result of collect
For example, Vec<_>
NicolasVidal
@NicolasVidal
Jun 16 2017 21:30
thx for your answer @vitvakatu, maybe the collect is not what I need, I just want to force an iteration without using for .. in
Ilya Bogdanov
@vitvakatu
Jun 16 2017 21:31
Actually, yes, there is no need in collect here
Try to remove it
Denis Lisov
@tanriol
Jun 16 2017 21:34
@vitvakatu If the collect is removed, the iterator will not be iterated at all.
@NicolasVidal You can use .collect::<Vec<_>>(); or use something else like fold or all.
NicolasVidal
@NicolasVidal
Jun 16 2017 21:37
thx for your answer @tanriol , but even with .collect::<Vec<_>>(); I've got the same error :
error: the type of this value must be known in this context
  --> src\main.rs:32:36
   |
32 |         .map(|i, f| println!("{}", f(i)))
   |                                    ^^^^
Denis Lisov
@tanriol
Jun 16 2017 21:38
@NicolasVidal And one more fix: |(i, f)|
You need not a function of two arguments, but a function of one argument which is a tuple.
NicolasVidal
@NicolasVidal
Jun 16 2017 21:39
ow damn ... ok that was what I was missing ... the tuple ^^ thank you very much @tanriol !
sorry for the inconvenience for a silly problem as this :)
it works perfectly now, thanks to you @vitvakatu and @tanriol !