Daiajo Tibdixious
Daiajo Tibdixious

Reputation: 1

rust by example: closure iterator any gives E0308

I've been trying the Rust by Example book code, but am stumped on https://doc.rust-lang.org/stable/rust-by-example/fn/closures/closure_examples/iter_any.html

error[E0308]: mismatched types
  --> Function.rs:81:95
81 |         fn any < F >  ( & mut self , mut f: F ) -> bool where F: FnMut ( Self::Item ) -> bool {}
   |                                                                                               ^^ expected bool, found ()

The full snippet I'm using is:

fn closure_iterator_any ( v1a: i32 , v1b: i32 , v1c: i32 , v2a: i32 , v2b: i32 , v2c: i32 )
  {
    pub trait Iterator
      {
        type Item ; // type of item being iterated over
        fn any < F >  ( & mut self , mut f: F ) -> bool where F: FnMut ( Self::Item ) -> bool {}
      }
    let vec1 = vec! [ v1a , v1b , v1c ] ;
    let vec2 = vec! [ v2a , v2b , v2c ] ;
    println! ( "2 in vector 1: {}" , vec1 . iter () . any ( |&x| x == 2 ) ) ; // iter returns &i32
    println! ( "2 in vector 2: {}" , vec2 . into_iter () . any ( |x| x == 2 ) ) ; // into returns i32

    let array1 = [ v1a , v1b , v1c ] ;
    let array2 = [ v2a , v2b , v2c ] ;
    println! ( "2 in array 1: {}" , array1 . iter () . any ( |&x| x == 2 ) ) ;
    println! ( "2 in array 2: {}" , array2 . into_iter () . any ( |&x| x == 2 ) ) ;

  }

I know there is excessive whitespace, when learning a new language I like to know where I can put space and where I can't. I don't really understand closures yet either.

Upvotes: 0

Views: 196

Answers (1)

SCappella
SCappella

Reputation: 10454

In the example, the part with the trait,

pub trait Iterator {
    // The type being iterated over.
    type Item;

    // `any` takes `&mut self` meaning the caller may be borrowed
    // and modified, but not consumed.
    fn any<F>(&mut self, f: F) -> bool
    where
        // `FnMut` meaning any captured variable may at most be
        // modified, not consumed. `Self::Item` states it takes
        // arguments to the closure by value.
        F: FnMut(Self::Item) -> bool,
    {}
}

is simply showing you the signature of the method. Specifically, it's telling you what arguments it takes, what generic types it uses, and what type it returns. It's not meant to be executed, and indeed is the source of the compile error you're seeing.

The specific error is saying that that any should return bool but right now it's returning () (the unit type which has a unique element). The reason for this is all in the little {} near the end. This is actually an empty function body which will implicitly return () if it doesn't have a return statement or anything else that might return.

If you just want to make the example compile, you could simply remove that chunk (the whole pub trait ... block) since it's not really part of the example. Even if you fix it and keep it in, the any used in the rest of the example won't be this one anyway, but rather the any method defined on the real Iterator trait in the standard library.

Upvotes: 2

Related Questions