Should you use const generic arguments where not needed?

⚓ Rust    📅 2025-12-08    👤 surdeus    👁️ 10      

surdeus

Warning

This post was published 61 days ago. The information described in this article may have changed.

const generic arguments are quite useful for functions that, for example, need the N declared on a [T; N]. However for functionality that need not consume a const, should const arguments be used? For code where the end use is expected to pass an integer that will never change for example, should a const argument be used? Take for example the following:

fn calculate<const X: usize, const Y: usize, const Z: usize>(other_arg: usize) -> SomeResult {
    // ...
}

The above function is expected to pass around X, Y and Z to various other functions as a const generic arg, all of which perform functionality that would compile fine regardless of whether they were a constant. So my question is, would the above benefit over the following:

fn calculate(x: usize, y: usize, z: usize, other_arg: usize) -> SomeResult {
    // ...
}

called with calculate(1, 2, 3, my_non_const_arg); (Note the const defined 1, 2, 3)?

1 post - 1 participant

Read full topic

🏷️ Rust_feed