While this is a trivial example, I have multiple generic functions which need to work across a few dozen possible values. The actual value is not known until runtime, as it is provided as an argument.
#![feature(generic_const_exprs)]
fn generic_division<const DIVISOR: u64>(denominator: u64) -> u64 {
denominator / DIVISOR
}
fn main() {
let denominator = 12;
for i in 1..5 {
let dividend = match i {
1 => generic_division::<1>(denominator),
2 => generic_division::<2>(denominator),
3 => generic_division::<3>(denominator),
4 => generic_division::<4>(denominator),
_ => panic!()
};
println!("{} / {} == {}", denominator, i, dividend);
}
}
Is there a better way than above? All other approaches have run into const
issues.
When you use
const
generic parameter, intent is to guarantee that parameter is known at compile time. Its not even syntactically possible to pass runtime value here. If you want to ensure your function can be constant folded and also accept runtime values, if needed, you can useconst fn
. Lets give an example.