第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

將可變的自引用傳遞給擁有對(duì)象的方法

將可變的自引用傳遞給擁有對(duì)象的方法

Git
慕妹3146593 2019-12-06 15:51:21
以下是一個(gè)簡(jiǎn)單的模擬,其中的場(chǎng)是一個(gè)矩形區(qū)域,其中有兩個(gè)彈跳的球。該Field結(jié)構(gòu)具有一個(gè)update方法,該方法調(diào)用update每個(gè)球。用它們的update方法,球需要根據(jù)它們的速度運(yùn)動(dòng)。但是他們還需要相互回應(yīng),以及領(lǐng)域的界限。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 {            radius: radius,            position: position,            velocity: velocity,        }    }    fn update(&mut self, field: &Field) {        // check collisions with walls        // and other objects    }}struct Field {    size: Vector2d,    balls: [Ball; 2],}impl Field {    fn new(size: Vector2d) -> Field {        let position_1 = Vector2d {            x: size.x / 3,            y: size.y / 3,        };        let velocity_1 = Vector2d { x: 1, y: 1 };        let position_2 = Vector2d {            x: size.x * 2 / 3,            y: size.y * 2 / 3,        };        let velocity_2 = Vector2d { x: -1, y: -1 };        let ball_1 = Ball::new(1, position_1, velocity_1);        let ball_2 = Ball::new(1, position_2, velocity_2);        Field {            size: size,            balls: [ball_1, ball_2],        }    }    fn update(&mut self) {        // this does not compile        self.balls[0].update(self);        self.balls[1].update(self);    }}如何獲得有關(guān)邊界和另一個(gè)球的信息,以獲取Ball結(jié)構(gòu)的更新功能?中的這些行Field::update不會(huì)編譯:self.balls[0].update(self);self.balls[1].update(self);出現(xiàn)以下錯(cuò)誤:error[E0502]: cannot borrow `*self` as immutable because `self.balls[..]` is also borrowed as mutable  --> src/main.rs:62:30   |62 |         self.balls[0].update(self);   |         -------------        ^^^^- mutable borrow ends here   |         |                    |   |         |                    immutable borrow occurs here   |         mutable borrow occurs here我了解,但是我不知道該如何解決。
查看完整描述

2 回答

?
慕尼黑的夜晚無(wú)繁華

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超6個(gè)贊

當(dāng)前,您的Ball結(jié)構(gòu)需要了解Field它所包含的內(nèi)容,以便能夠自我更新。這不會(huì)編譯,因?yàn)榻Y(jié)果將是循環(huán)引用與變異結(jié)合在一起。您可以通過(guò)使用Cell或RefCell(后者會(huì)降低性能)來(lái)完成這項(xiàng)工作,但最好以不同的方式構(gòu)造代碼。讓該Field結(jié)構(gòu)檢查并解決Ball- Ball和Ball- Wall沖突。該Ball結(jié)構(gòu)的update函數(shù)可以處理更新Ball的位置。


// Ball's update function

fn update(&mut self) {

    // update position

}


// Field's update function

fn update(&mut self) {

    for ball in self.balls.iter_mut() {

        ball.update();

    }


    // check for collisions


    // resolve any collisions

}


查看完整回答
反對(duì) 回復(fù) 2019-12-06
?
智慧大石

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超3個(gè)贊

這是一個(gè)較小的示例:


struct Ball {

    size: u8,

}


impl Ball {

    fn update(&mut self, field: &Field) {}

}


struct Field {

    ball: Ball,

}


impl Field {

    fn update(&mut self) {

        self.ball.update(self)

    }

}

問(wèn)題

當(dāng)您傳遞對(duì)的引用時(shí)Field,您將保證Field不能更改(“不可變引用” 的不可變部分)。但是,此代碼也試圖改變它的一部分:球!self或field在實(shí)施中應(yīng)參考哪個(gè)參考Ball::update?


解決方案:僅使用您需要的字段

您可以將結(jié)構(gòu)所需的部分與不需要的部分分開(kāi),update并在調(diào)用update函數(shù)之前使用它們:


struct Ball {

    size: u8,

}


impl Ball {

    fn update(&mut self, field: &u8) {}

}


struct Field {

    players: u8,

    ball: Ball,

}


impl Field {

    fn update(&mut self) {

        self.ball.update(&self.players)

    }

}

您甚至可以將這些零星的引用捆綁到一個(gè)整齊的包中:


struct Ball {

    size: u8,

}


impl Ball {

    fn update(&mut self, field: BallUpdateInfo) {}

}


struct BallUpdateInfo<'a> {

    players: &'a u8,

}


struct Field {

    players: u8,

    ball: Ball,

}


impl Field {

    fn update(&mut self) {

        let info = BallUpdateInfo { players: &self.players };

        self.ball.update(info)

    }

}

或重組您的包含結(jié)構(gòu)以將信息與開(kāi)頭分開(kāi):


struct Ball {

    size: u8,

}


impl Ball {

    fn update(&mut self, field: &UpdateInfo) {}

}


struct UpdateInfo {

    players: u8,

}


struct Field {

    update_info: UpdateInfo,

    ball: Ball,

}


impl Field {

    fn update(&mut self) {

        self.ball.update(&self.update_info)

    }

}

解決方案:從中刪除成員 self

您也可以采用其他方法,Ball從中進(jìn)行刪除,F(xiàn)ield然后再對(duì)其進(jìn)行任何更改。如果您可以輕松/廉價(jià)地制造Ball,請(qǐng)嘗試更換它:


use std::mem;


#[derive(Default)]

struct Ball {

    size: u8,

}


impl Ball {

    fn update(&mut self, field: &Field) {}

}


struct Field {

    ball: Ball,

}


impl Field {

    fn update(&mut self) {

        let mut ball = mem::replace(&mut self.ball, Ball::default());

        ball.update(self);

        self.ball = ball;

    }

}

如果您不容易創(chuàng)建新值,則可以使用Optionand take:


struct Ball {

    size: u8,

}


impl Ball {

    fn update(&mut self, field: &Field) {}

}


struct Field {

    ball: Option<Ball>,

}


impl Field {

    fn update(&mut self) {

        if let Some(mut ball) = self.ball.take() {

            ball.update(self);

            self.ball = Some(ball);

        }

    }

}

解決方案:運(yùn)行時(shí)檢查

您可以通過(guò)以下方式將借閱檢查移至運(yùn)行時(shí)而不是編譯時(shí)RefCell:


use std::cell::RefCell;


struct Ball {

    size: u8,

}


impl Ball {

    fn update(&mut self, field: &Field) {}

}


struct Field {

    ball: RefCell<Ball>,

}


impl Field {

    fn update(&mut self) {

        self.ball.borrow_mut().update(self)

    }

}


查看完整回答
反對(duì) 回復(fù) 2019-12-06
  • 2 回答
  • 0 關(guān)注
  • 467 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)