Tag: 借用检查器

通过可变的自引用方法拥有对象

下面是一个简单的模拟,其中一个场地是一个矩形区域,两个球在其中弹跳。 Field结构体有一个update方法,它调用每个球的update 。 在update方法中,球需要根据其速度移动。 但他们也需要相互作出反应,以及领域的界限: fn main() { let mut field = Field::new(Vector2d { x: 100, y: 100 }); field.update(); } #[derive(Copy, Clone)] struct Vector2d { x: i32, y: i32, } struct Ball { radius: i32, position: Vector2d, velocity: Vector2d, } impl Ball { fn new(radius: i32, position: Vector2d, velocity: Vector2d) -> Ball { Ball […]

为什么我不能在同一个结构中存储一个值和一个对这个值的引用?

我有一个价值,我想存储这个价值,并在我的types的价值内引用的价值: struct Thing { count: u32, } struct Combined<'a>(Thing, &'a u32); fn make_combined<'a>() -> Combined<'a> { let thing = Thing { count: 42 }; Combined(thing, &thing.count) } 有时,我有一个价值,我想存储这个值,并在同一结构中存储对这个值的引用: struct Combined<'a>(Thing, &'a Thing); fn make_combined<'a>() -> Combined<'a> { let thing = Thing::new(); Combined(thing, &thing) } 有时,我甚至没有参考价值,我得到了同样的错误: struct Combined<'a>(Parent, Child<'a>); fn make_combined<'a>() -> Combined<'a> { let parent […]