What Is the Difference Between (&V). Func() and &V. Func()?

What is (&v) actually doing in this code?

let v = vec!["hello", "viktor"];
let mut iterator = (&v).into_iter(); // Iter<&str>
let mut iterator = &v.into_iter(); // &IntoIter<&str>

How it is changing what is returned from .into_iter(). Why is the result different?

1

1 Answer

This is a precedence issue. With &v.into_iter(), the compiler understands &(v.into_iter()) not (&v).into_iter(), just like when you write 1+2*3, the compiler understands 1+(2*3), and not (1+2)*3.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest