Читаем Rust by Example полностью

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

<p id="see_also_27"><strong><a l:href="#see_also_27">See also:</a></strong></p>

Fn, FnMut, Generics and impl Trait.

<p id="examples_in_std"><strong><a l:href="#examples_in_std">Examples in</a><a l:href="#examples_in_std">std</a></strong></p>

This section contains a few examples of using closures from the std library.

<p id="iteratorany"><strong><a l:href="#iteratorany">Iterator::any</a></strong></p>

Iterator::any is a function which when passed an iterator, will return true if any element satisfies the predicate. Otherwise false. Its signature:

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(&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 {}

}

fn main() {

let vec1 = vec![1, 2, 3];

let vec2 = vec![4, 5, 6];

// `iter()` for vecs yields `&i32`. Destructure to `i32`.

println!("2 in vec1: {}", vec1.iter() .any(|&x| x == 2));

// `into_iter()` for vecs yields `i32`. No destructuring required.

println!("2 in vec2: {}", vec2.into_iter().any(| x| x == 2));

let array1 = [1, 2, 3];

let array2 = [4, 5, 6];

// `iter()` for arrays yields `&i32`.

println!("2 in array1: {}", array1.iter() .any(|&x| x == 2));

// `into_iter()` for arrays unusually yields `&i32`.

println!("2 in array2: {}", array2.into_iter().any(|&x| x == 2));

}

הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

<p id="see_also_28"><strong><a l:href="#see_also_28">See also:</a></strong></p>

std::iter::Iterator::any

<p id="searching_through_iterators"><strong><a l:href="#searching_through_iterators">Searching through iterators</a></strong></p>

Iterator::find is a function which iterates over an iterator and searches for the first value which satisfies some condition. If none of the values satisfy the condition, it returns None. Its signature:

pub trait Iterator {

// The type being iterated over.

type Item;

// `find` takes `&mut self` meaning the caller may be borrowed

// and modified, but not consumed.

fn find

(&mut self, predicate: P) -> Option where

// `FnMut` meaning any captured variable may at most be

// modified, not consumed. `&Self::Item` states it takes

// arguments to the closure by reference.

P: FnMut(&Self::Item) -> bool {}

}

fn main() {

let vec1 = vec![1, 2, 3];

Перейти на страницу:

Похожие книги