这些迭代向量的方法有什么区别吗? [英] Is there any difference in these approaches to iterate through a vector?

查看:38
本文介绍了这些迭代向量的方法有什么区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there any difference in the following approaches to iterate through a vector? Both methods successfully iterate.

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

推荐答案

没有区别,没有.

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

The second one is impl<'a, T> IntoIterator for &'a Vec<T>, and it just call the first one (Vec::iter). Since the method is so short, there's roughly 100% chances it's going to get inlined and you'll get the same result (with an intermediate function call if you're compiling without optimisations but that's about it).

这篇关于这些迭代向量的方法有什么区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆