What is the type of iterator returned by this Rust function?

So I have an example code:

use std::iter::Filter;
use std::slice::Iter;

fn main() {
    let numbers = vec![12i32, 26, 31, 56, 33, 16, 81];

    for number in ends_in_six(numbers) {
        println!("{}", number);
    }
}

fn ends_in_six(numbers: Vec<i32>) /* -> ??? */ {
    numbers.iter().filter(|&n| *n % 10 == 6)
}

I am trying to return an iterator that was always quite hairy in Rust from what I put together. Executing the code here gives me this error:

<anon>:13:5: 13:45 error: mismatched types:
 expected `()`,
    found `core::iter::Filter<core::slice::Iter<'_, i32>, [closure <anon>:13:27: 13:44]>`
(expected (),
    found struct `core::iter::Filter`) [E0308]
<anon>:13     numbers.iter().filter(|&n| *n % 10 == 6)
              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Now that I have worked through this (and based on my relatively limited knowledge of how it all works), it seems I should do something like:

fn ends_in_six<'a>(numbers: Vec<i32>) -> Filter<Iter<'a, i32>, /* ??? */> {

But now I'm stuck again, because I'm assigned [closure <anon>:13:27: 13:44]instead of the actual type. Even when I tried to use the function here to try to figure out the type, they gave me:

core::iter::Filter<core::slice::Iter<i32>, [closure((&&i32,)) -> bool]>

So, trying to figure it out on my own and based on the previous line, I tried:

fn ends_in_six<'a>(numbers: Vec<i32>) -> Filter<Iter<'a, i32>, Fn(&&i32) -> bool> {

, Fn (.. Sized). , , .

EDIT: :

fn ends_in_six<'a, F>(numbers: Vec<i32>) -> Filter<Iter<'a, i32>, F>
  where F: Fn(&&i32) -> bool {

:

<anon>:7:19: 7:30 error: unable to infer enough type information about `_`; type annotations required [E0282]
<anon>:7     for number in ends_in_six(numbers) {
                           ^~~~~~~~~~~
<anon>:14:32: 14:34 error: the type of this value must be known in this context
<anon>:14     numbers.iter().filter(|&n| *n % 10 == 6)
                                         ^~
<anon>:14:27: 14:44 error: mismatched types:
 expected `F`,
    found `[closure <anon>:14:27: 14:44]`
(expected type parameter,
    found closure) [E0308]
<anon>:14     numbers.iter().filter(|&n| *n % 10 == 6)
                                    ^~~~~~~~~~~~~~~~~
+4
1

( - Fn, FnMut FnOnce, ), .

, , , , .

- - (Box<Fn(&&i32) -> bool>), Box::new(|&&n| n % 10 == 6). , , , .

, , , , , , .

use std::iter::Filter;
use std::slice::Iter;

fn main() {
    let numbers = vec![12i32, 26, 31, 56, 33, 16, 81];

    for number in ends_in_six(numbers) {
        println!("{}", number);
    }
}

fn ends_in_six(numbers: Vec<i32>) Filter<Iter<'a, i32>, fn(&&i32) -> bool> {
    fn filterer(&&n: &&i32) -> bool { n % 10 == 6 }
    numbers.iter().filter(filterer as fn(&&i32) -> bool)
}
+2

All Articles