[英]How to return a reference to a value from Hashmap wrappered in Arc and Mutex in Rust?
[英]Rust -- how to return a value from a HashMap
这是一个小function:
pub fn test_return_value() -> Box<dyn RoomInterface> {
let mut r = HashMap::<String, Box<dyn RoomInterface>>::new();
// blah blah blah, do stuff with the HashMap
r.get("test_key").unwrap()
}
这不会编译并说:
37 | pub fn test_return_value() -> Box<dyn RoomInterface> {
| ---------------------- expected `Box<(dyn room::RoomInterface + 'static)>` because of return type
38 | let mut r = HashMap::<String, Box<dyn RoomInterface>>::new();
39 | r.get("test_key").unwrap()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `Box`, found reference
这个错误是什么意思? 如何返回存储在 HashMap 中的值?
使用remove
而不是get
帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.