一个可变引用的问题
从 rust 社区看到的一个问题,原文地址:https://users.rust-lang.org/t/calling-a-mu...
问题描述#
题主实现如下代码,报错:
use std::collections::HashMap;
struct MyData {
d: f32
}
struct Foo {
d: HashMap<String, MyData>
}
impl Foo {
fn get(&mut self, s: &str) -> Option<&MyData> {
if !self.d.contains_key(s) {
self.d.insert(s.to_string(), MyData{ d: 0.0 });
}
self.d.get(s)
}
}
fn main() {
let mut foo = Foo {
d: HashMap::new()
};
let mut saved = Vec::new();
for _i in 1..3 {
let l = foo.get("a");
saved.push(l);
}
}
问题解决#
方法一#
use std::collections::HashMap;
#[derive(Clone, Copy)]
struct MyData {
d: f32
}
struct Foo {
d: HashMap<String, MyData>
}
impl Foo {
fn get(&mut self, s: &str) -> Option<MyData> {
if !self.d.contains_key(s) {
self.d.insert(s.to_string(), MyData{ d: 0.0 });
}
self.d.get(s).copied()
}
}
fn main() {
let mut foo = Foo {
d: HashMap::new()
};
let mut saved = Vec::new();
for _i in 1..3 {
let l = foo.get("a");
saved.push(l);
}
}
方法二#
use std::collections::HashMap;
struct Foo {
d: HashMap<String, f32>
}
impl Foo {
fn get(&mut self, s: &str) -> Option<f32> {
if !self.d.contains_key(s) {
self.d.insert(s.to_string(), 0.0);
}
self.d.get(s).copied()
}
}
fn main() {
let mut foo = Foo {
d: HashMap::new()
};
let mut saved = Vec::new();
for _i in 1..3 {
let l = foo.get("a");
saved.push(l);
}
}
推荐文章: