-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Suggest changing &mut Vec<_>
parameters to &mut [_]
when only slice methods are used.
#6406
Comments
I think its better if clipyy suggests using an iterator over the Vector. Changing a vec to an array might involve more code change than chaning a loop to an iterator. |
Note that I'm not suggesting an array here, but a slice -- which is what code like clippy will already suggest replacing the indexing with |
Ok. But I think that proposing |
I see this as a both, not an either-or. I totally agree that
should change to
But this issue is just about one part. They're separable. |
@Volker-Weissmann it does not require a change other than the type of the function parameter: fn use_for(vec: &mut [usize]) {
for i in 0..vec.len() {
vec[i] = i;
}
}
fn main() {
let mut v = vec![1, 2, 3, 4, 5];
use_for(&mut v);
} You can see in the playground too that Clippy will already suggest EDIT: @scottmcm beat me to it :) |
Clippy already suggests changing But it intentionally doesn't lint |
Thanks, @flip1995 -- I hadn't checked that one. I've edited the OP. |
This mistake comes up fairly frequently on URLO, such as
https://users.rust-lang.org/t/iterators-vs-index-loops-performance/52131?u=scottmcm
https://users.rust-lang.org/t/we-all-know-iter-is-faster-than-loop-but-why/51486/3?u=scottmcm
What it does
&mut Vec<_>
or[ed: the&Vec<_>
&Vec<_>
one already exists]Categories (optional)
perf
&style
both seem appropriateWhat is the advantage of the recommended code over the original code
Vec
s instead of only being able to pass entire onesVec
For example:
Drawbacks
None.
Example
Could be written as:
The text was updated successfully, but these errors were encountered: