4
內:訪問HashMap的字段值作爲與MUT給出一個簡單的結構類似這樣的結構IMPL
struct Server {
clients: HashMap<usize, Client>
}
什麼是訪問Client
作爲&mut
的最佳方式?請看下面的代碼:
use std::collections::HashMap;
struct Client {
pub poked: bool
}
impl Client {
pub fn poked(&self) -> bool {
self.poked
}
pub fn set_poked(&mut self) {
self.poked = true;
}
}
struct Server {
clients: HashMap<usize, Client>
}
impl Server {
pub fn poke_client(&mut self, token: usize) {
let client = self.clients.get_mut(&token).unwrap();
self.poke(client);
}
fn poke(&self, c: &mut Client) {
c.set_poked();
}
}
fn main() {
let mut s = Server { clients: HashMap::new() };
s.clients.insert(1, Client { poked: false });
s.poke_client(1);
assert!(s.clients.get(&1).unwrap().poked() == true);
}
只有兩個選擇我看到的是使用RefCell
/Cell
內部客戶端,這讓事情看起來很可怕:
pub struct Client {
nickname: RefCell<Option<String>>,
username: RefCell<Option<String>>,
realname: RefCell<Option<String>>,
hostname: RefCell<Option<String>>,
out_socket: RefCell<Box<Write>>,
}
或者包裹clients
在RefCell
,其中使得不可能有這樣的簡單方法Server
:
pub fn client_by_token(&self, token: usize) -> Option<&Client> {
self.clients_tok.get(&token)
}
迫使我使用閉包(例如with_client_by_token(|c| ...)
)。
生鏽的風格指南是4空間縮進。 – Shepmaster