Does const accessing speed (const EX: Ex = Ex::new()) is faster than it dynamic creating and accessing?
Purely for access, there shouldn't be a major difference either way. As a microoptimization, there's no general rule, because it depends on the context and what the optimizer can do.
But if you're trying to optimize code by adding const
everywhere, it may be a bad idea. It may make code more bloated, because it will create a new copy of the const
value every time it's used. This can also lead to surprising behavior and bugs.
const
items are not behaving like let
variables:
fn main() {
let a = b + 1;
const b: i32 = 2;
}
fn main() {
const vec: Vec<u8> = Vec::new();
vec.push(1);
assert!(vec.is_empty());
}
3 Likes
Thanks for the explanation.
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.