【问题标题】:Is there any difference in these approaches to iterate through a vector? [duplicate]这些迭代向量的方法有什么不同吗? [复制]
【发布时间】:2020-10-06 03:59:19
【问题描述】:

以下遍历向量的方法有什么不同吗?两种方法都成功迭代。

let vo = vec![30, 50, 70, 80];

方法一

for uu in vo.iter() {
    println!("uu {}", uu);
}
println!("vo 1 {:?}", vo);

方法二

for uu in &vo {
    println!("{}", uu);
}
println!("vo 2 {:?}", vo);

【问题讨论】:

标签: rust


【解决方案1】:

没有区别,没有。

第二个是impl<'a, T> IntoIterator for &'a Vec<T>it just call the first one (Vec::iter)。由于该方法非常短,因此它有大约 100% 的机会被内联,并且您将获得相同的结果(如果您在没有优化的情况下进行编译,则使用中间函数调用,仅此而已)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-20
    • 2017-06-25
    • 1970-01-01
    • 1970-01-01
    • 2011-11-22
    • 2018-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多