3 ms·
It's more complaining that Rust is verbose when one wants to store small integers in structs but have intermediate computation use machine width integers Impli
by __s 4y ago
It's more complaining that Rust is verbose when one wants to store small integers in structs but have intermediate computation use machine width integers
Implicit widening doesn't tend to cause bugs. I don't think it'd be so bad if vec[u8] implicitly worked without needing vec[u8 as usize]
- estebank 4y agoFWIW, you can have a newtype wrapper around Vec that provides indexing access with any type you want. If you are porting or writing code that uses i32 for indices for some reason, then you use the wrapper instead of Vec with no runtime cost and no inline type conversions. This hasn't been solved at the language level yet because if we `impl Idx for u8` then inference in `vec[42]` breaks. Making inference support that in a principled manner will require a lot of work, and doing it in a hacky way might lock us into a suboptimal place that we haven't fully explored yet. I want to see some solution here at some point.
- colejohnson66 4y agoYou wouldn’t have to ‘impl Idx for u8’ if implicit upconversion was a thing. If it was, we could do this with no issues: let idx: u8 = 42; let val = vec[idx]; As GP said, the current “solution” is to do ‘idx as usize’ which is verbose.