提问人:hope_is_grim 提问时间:10/24/2023 最后编辑:cafce25hope_is_grim 更新时间:10/24/2023 访问量:70
如何使用共享可变结构字段在相同的方法中读取和写入
How to use a share mutable struct field to read and write to in the same method
问:
我正在尝试在我的结构中使用共享来进行一些计算Vec3
Mat4
struct Vec3 {
x: f32;
y: f32;
}
struct Mat4 {
values: [f32;16];
tmp_vec3: Vec3;
}
我里面有这个方法:Mat4
impl Mat4 {
pub fn set_to_look_at(&mut self, position: &Vec3, target: &Vec3, up: &Vec3) -> &mut Self {
let tmp_vec = &mut self.tmp_vec3;
tmp_vec.set_from(target).sub(position);
self.look_at(position, &tmp_vec, up)
}
}
此代码无法编译,抱怨.cannot borrow '*self' as mutable more than once at a time
这是什么模式?
答: 暂无答案
评论
set_from
sub
look_at
self.look_at(position, &tmp_vec, up)
tmp_vec
self
look_at
self.tmp_vec3
Vec3
足够小,您可能应该只制作它并传递值而不是引用。在 64 位计算机上,它的大小与指针的大小相同,因此复制它的工作量与传递引用相同,但不进行间接操作。所以没有缺点,只有好处,包括解决这个问题。Copy
Vec3
Vec3
Copy
tmp_vec
self.tmp_vec3
self.look_at
tmp_vec
self.look_at
self