如何在没有显式强制转换的情况下使用整数类型(除usize之外)索引向量? [英] How to index vectors with integer types (besides usize), without explicit cast?

查看:48
本文介绍了如何在没有显式强制转换的情况下使用整数类型(除usize之外)索引向量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时索引需要紧密包装(例如,网格几何),将索引存储为 u32 而不是 usize很有用



有没有一种方法可以在Rust中索引向量,而不必显式转换为 usize 每次?例如:

  vector [some_u32 asize]; 

在单个实例中进行铸造不是问题,只是在经常需要时乏味。



有没有一种方法可以避免不必在此处投射?

解决方案

否。



如果它是您自己的类型,则可以实现 Index< u32> ,但事实并非如此,您可以



如果您真的在病理学上反对强制转换索引,则可以编写一个执行强制转换的适配器类型,但这有点愚蠢。 / p>

There are times when indices need to be tightly packed (mesh geometry for example), where its useful to store indices as u32 instead of usize.

Is there a way to index a vector in Rust without having to explicitly cast to usize every time? eg:

vector[some_u32 as usize];

Casting in single instances isn't a problem, its just tedious when needed frequently.

Is there a way to avoid having to cast here?

解决方案

No.

If it were your own type, you could implement Index<u32>, but it isn't and you can't.

If you're really, pathologically opposed to casting the index, you could write an adaptor type that does the cast, but that's getting a bit silly.

这篇关于如何在没有显式强制转换的情况下使用整数类型(除usize之外)索引向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆