當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Rust Iterator.cloned用法及代碼示例

本文簡要介紹rust語言中 std::iter::Iterator.cloned 的用法。

用法

fn cloned<'a, T>(self) -> Cloned<Self> where    T: 'a + Clone,    Self: Iterator<Item = &'a T>,

創建一個迭代器,其中 clone 是其所有元素。

當您有一個超過 &T 的迭代器時,這很有用,但您需要一個超過 T 的迭代器。

例子

基本用法:

let a = [1, 2, 3];

let v_cloned: Vec<_> = a.iter().cloned().collect();

// cloned is the same as .map(|&x| x), for integers
let v_map: Vec<_> = a.iter().map(|&x| x).collect();

assert_eq!(v_cloned, vec![1, 2, 3]);
assert_eq!(v_map, vec![1, 2, 3]);

相關用法


注:本文由純淨天空篩選整理自rust-lang.org大神的英文原創作品 std::iter::Iterator.cloned。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。