Are the following two examples equivalent?
Example 1:
let x = String::new();
let y = &x[..];
Example 2:
let x =
They are completely the same for String
and Vec
.
The [..]
syntax results in a call to Index
and it's not just sugar for [0..collection.len()]
. The latter would introduce the cost of bound checking. Gladly this is not the case in Rust so they both are equally fast.
Relevant code:
self
which triggers the deref coercion thus executes exactly the same code as just deref
)