vec! allocation used where an array would do
ID |
rust.performance.useless_vec |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Inefficient Api Usage |
Language |
Rust |
Description
vec! allocates on the heap, and this value is only iterated or borrowed as a slice, so the allocation is never used as a growable collection. Use an array literal ([1, 2, 3]) or a slice reference (&[1, 2, 3]) instead.
Rationale
vec! allocates on the heap, and this value is only iterated or borrowed as a slice, so the allocation is never used as a growable collection. Use an array literal ([1, 2, 3]) or a slice reference (&[1, 2, 3]) instead.
The following code illustrates the pattern detected by this rule:
fn iterate() {
// FLAGGED: vec! allocation used where an array would do
for code in vec![200, 301, 404] {
println!("{}", code);
}
}