Rust代码示例——9.2.6.在标准库中的示例

本章包含几个在标准库(std)中使用闭包的示例。

9.2.6.1 Iterator::any

Iterator::any是一个函数,当它传递一个迭代器作为参数时,当元素满足其指定的类型,返回true,否则返回false。其特性代码的格式如下:

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;

}
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));


    // `iter()` only borrows `vec1` and its elements, so they can be used again

    println!("vec1 len: {}", vec1.len());

    println!("First element of vec1 is: {}", vec1[0]);

    // `into_iter()` does move `vec2` and its elements, so they cannot be used again

    // println!("First element of vec2 is: {}", vec2[0]);

    // println!("vec2 len: {}", vec2.len());

    // TODO: uncomment two lines above and see compiler errors.


    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 yields `i32`.

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

}

9.2.6.2 通过迭代器进行搜索

Iterator::find也是一个函数,它使用迭代器变量其成员,找出与指定条件相匹配的第一个值。如果没有找到返回None。它的签名代码如下:

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<P>(&mut self, predicate: P) -> Option<Self::Item> 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];

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


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

    let mut iter = vec1.iter();

    // `into_iter()` for vecs yields `i32`.

    let mut into_iter = vec2.into_iter();


    // `iter()` for vecs yields `&i32`, and we want to reference one of its

    // items, so we have to destructure `&&i32` to `i32`

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

    // `into_iter()` for vecs yields `i32`, and we want to reference one of

    // its items, so we have to destructure `&i32` to `i32`

    println!("Find 2 in vec2: {:?}", into_iter.find(| &x| x == 2));


    let array1 = [1, 2, 3];

    let array2 = [4, 5, 6];


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

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

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

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值