t_wの輪郭

Feedlyでフォローするボタン
RustIteratorVec配列イテレータ
mapmapの試し『iter()とinto_iter()の違いとちょっとした落とし穴』『iter()とinto_iter()の違いを整理した』filterfilterの試し配列の各要素を取得するiter()

mapの試し

2022/2/26 16:23:00
let v1 = vec![1, 2, 3];
let v2:Vec<u32> = v1.iter().map(|x|{x + 1}).collect();
assert_eq!(v1, vec![1, 2, 3]);
assert_eq!(v2, vec![2, 3, 4]);
let v1:Vec<String> = vec![String::from("Hoge"), String::from("Fuga"), String::from("Foo")];
let v2:Vec<String> = v1.iter().map(|x|{format!("{}{}", x, x)}).collect();
assert_eq!(v1, vec![String::from("Hoge"), String::from("Fuga"), String::from("Foo")]);
assert_eq!(v2, vec![String::from("HogeHoge"), String::from("FugaFuga"), String::from("FooFoo")]);

filterの試し

2022/2/26 16:17:00
let v1:Vec<String> = vec![String::from("Hoge"), String::from("Fuga"), String::from("Foo")];
let v2:Vec<&String> = v1.iter().filter(|x|{x.len() == 3}).collect();
assert_eq!(v1, vec![String::from("Hoge"), String::from("Fuga"), String::from("Foo")]);
assert_eq!(v2, vec![&String::from("Foo")]);

map

2022/2/26 15:41:00
let a = [10, 20, 30, 40, 50];

for element in a.iter() {
    println!("the value is: {}", element);
}
/*
the value is: 10
the value is: 20
the value is: 30
the value is: 40
the value is: 50
*/