【发布时间】:2020-11-28 04:28:17
【问题描述】:
更新
我真正的问题是由于我的 IDE 自动导入了use std::borrow::{Borrow, BorrowMut};。
有了这一行,接受的答案also doesn't compile。
解决方案是移除线路。
我收到以下错误消息:
15 | instance_context.into_inner().instances = Some(vec![String::from("abc")]);
| ^^^^^^^^^^^^^^^^ move occurs because value has type `RefCell<InstanceContext>`, which does not implement the `Copy` trait
我不知道为什么,也不知道如何修复代码。
#![allow(dead_code)]
#![allow(unused_variables)]
use std::rc::Rc;
use std::cell::RefCell;
struct InstanceContext {
id: i32,
instances: Option<Vec<String>>,
}
fn main() {
let instance_context = Rc::new(RefCell::new(InstanceContext { id: 5, instances: None }));
// clojures are created that use instance_context, which does not yet have 'instances' set
instance_context.into_inner().instances = Some(vec![String::from("abc")]);
}
【问题讨论】:
标签: rust