Reputation: 3416
Currently I have the following
const WIDTH : u16 = 1920;
const HEIGHT: u16 = 1080;
const PIXELS : usize = 1920 * 1080;
It would be nicer to have something like
const WIDTH : u16 = 1920;
const HEIGHT: u16 = 1080;
const PIXELS : usize = usize::from(WIDTH) * usize::from(HEIGHT);
But this will not compile. I assume this is because const
are compile time variables and can't rely on the execution of usize::from
. Is there an alternative?
Upvotes: 2
Views: 528
Reputation: 13548
usize::from
is not a constant function. If it were, your example would compile just fine. Instead, you can use simply cast the variables, which is all usize::from
does internally:
const WIDTH : u16 = 1920;
const HEIGHT: u16 = 1080;
const PIXELS: usize = (WIDTH as usize) * (HEIGHT as usize);
Upvotes: 5