<story><title>Making Rust a Better Fit for Cheri and Other Platforms</title><url>https://tratt.net/laurie/blog/2022/making_rust_a_better_fit_for_cheri_and_other_platforms.html</url></story><parent_chain><item><author>davidhyde</author><text>It would also be nice to be able to use unsigned types (like u8, u16 and u32) to index into slices and arrays up until usize (usually a u32 or u64). Using a usize often seems wasteful for indexing into small arrays and slices and casting makes the code look ugly (also dangerous because the effect of casting is checked by the developer, not the compiler). Admittedly, this would have the downside of having code that compiles on one architecture but not another so it’s probably not worth it.</text></item></parent_chain><comment><author>ATsch</author><text>&amp;gt; Using a usize often seems wasteful for indexing into small arrays and slices&lt;p&gt;The opposite is often the case, as the architecture may not support offsetting a pointer by those sizes, requiring lots of casting in tight loops. Making that explicit makes the programmer aware of this.&lt;p&gt;This is one major reason that C compiler developers want all overflow to be undefined behavior, as it allows them to upgrade for loops that use smaller index types to larger ones.</text></comment>
<story><title>Making Rust a Better Fit for Cheri and Other Platforms</title><url>https://tratt.net/laurie/blog/2022/making_rust_a_better_fit_for_cheri_and_other_platforms.html</url></story><parent_chain><item><author>davidhyde</author><text>It would also be nice to be able to use unsigned types (like u8, u16 and u32) to index into slices and arrays up until usize (usually a u32 or u64). Using a usize often seems wasteful for indexing into small arrays and slices and casting makes the code look ugly (also dangerous because the effect of casting is checked by the developer, not the compiler). Admittedly, this would have the downside of having code that compiles on one architecture but not another so it’s probably not worth it.</text></item></parent_chain><comment><author>ekidd</author><text>Rust really doesn&amp;#x27;t do implicit numeric conversion, and I suspect it would be hard to retrofit. But you could write a function i(...) that promoted u8, etc, to usize:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    v[i(my_u8_index)]
&lt;/code&gt;&lt;/pre&gt;
This could be implemented as a compile-time check, with no runtime errors. You&amp;#x27;d have to limit which types you support as indices if you want to be portable to platforms with tiny pointers, of course.&lt;p&gt;I guess something like this might be useful inside of specialized library code that worked with lots of small vectors.&lt;p&gt;But in general, Rust heavily favors explicit over implicit in many areas. If you want lots of automatic implicit behavior, something like Scala might be a better choice?</text></comment>