Rust语言编程实例100题-076
题目:Rust迭代器专项练习。迭代器模式是将遍历数据集合的行为抽象为单独的迭代对象,这样在遍历集合时可以把集合中所有元素按顺序传递给处理逻辑。
要求:使用迭代器中的collect消费器和map适配器,将数组[1,5,7,9,11,23,24]每个数字乘以3 后输出。
**程序分析:**无。
知识点:Rust迭代器
参考程序代码:
fn main() {
let num: Vec<i32> = vec![1, 5, 7, 9, 11, 23, 24];
let result: Vec<i32> = num.iter().map(|x| { *x * 3 }).collect();
dbg!(result);
}
程序执行结果:
result = [
3,
15,
21,
27,
33,
69,
72,
]
Process finished with exit code 0