小编Epi*_*ato的帖子

通用结构传染媒介在铁锈的

我在Rust中创建一个实体组件系统,我希望能够Vec为每种不同的Component类型存储一个组件:

pub trait Component {}

struct ComponentList<T: Component> {
    components: Vec<T>,
}
Run Code Online (Sandbox Code Playgroud)

是否有可能创建这些ComponentLists 的集合?

struct ComponentManager {
    component_lists: Vec<ComponentList<_>>, // This does not work
}
Run Code Online (Sandbox Code Playgroud)

这旨在使检索某种Component类型的列表更快,因为某种类型的组件的所有实例都是相同的ComponentList.

generics rust

6
推荐指数
1
解决办法
2946
查看次数

有没有办法在Rust中获取`struct`的地址?

我曾尝试使用原始指针强制转换my_struct as *const usize,但这会导致non-scalar cast错误.找到基元的地址时,原始指针似乎工作正常,但不是自定义structs.

struct rust

5
推荐指数
2
解决办法
2871
查看次数

反向迭代,for循环或while循环哪个更快?

我正在尝试memmove在Rust中实现标准函数,我想知道哪种方法对于向下迭代更快(其中src< dest):

for i in (0..n).rev() {
    //Do copying
}
Run Code Online (Sandbox Code Playgroud)

要么

let mut i = n;
while i != 0 {
    i -= 1;
    // Do copying
}
Run Code Online (Sandbox Code Playgroud)

rev()for环版本显著慢下来?

for-loop while-loop rust memmove

4
推荐指数
1
解决办法
594
查看次数

在Java中过度使用get()是否效率低下?

我想知道在对象上过度使用get()或在变量中存储get()的返回是否更有效并使用它.例如,执行此操作会更有效:

someObject.setColor(otherObject.getColor().r, otherObject.getColor().g, 
         otherObject.getColor().b);
Run Code Online (Sandbox Code Playgroud)

或者将它存储在这样的变量中

Color color = otherObject.getColor();
someObject.setColor(color.r, color.g, color.b);
Run Code Online (Sandbox Code Playgroud)

java get set

3
推荐指数
1
解决办法
66
查看次数

标签 统计

rust ×3

for-loop ×1

generics ×1

get ×1

java ×1

memmove ×1

set ×1

struct ×1

while-loop ×1