gpt4 book ai didi

rust - 无法从 &mut self 借用文件(错误消息 : cannot move out of borrowed content)

转载 作者:行者123 更新时间:2023-11-29 07:41:30 24 4
gpt4 key购买 nike

use std::fs::File;
use std::io::Read;

pub struct Foo {
maybe_file: Option<File>,
}

impl Foo {
pub fn init(&mut self) {
self.maybe_file = Some(File::open("/proc/uptime").unwrap());
}

pub fn print(&mut self) {
let mut file = self.maybe_file.unwrap();
let mut s = String::new();
file.read_to_string(&mut s).unwrap();
println!("Uptime: {}", s);
}
}

fn main() {}

编译它会给我:

error[E0507]: cannot move out of borrowed content
--> src/main.rs:14:24
|
14 | let mut file = self.maybe_file.unwrap();
| ^^^^ cannot move out of borrowed content

为什么会这样?我该怎么做才能解决它?

最佳答案

self类型为 &mut Fooprint ,也就是说,它是对 Foo 类型值的借用可变引用. Rust 中的类型默认移动所有权,也就是说,按值取值将使源静态无效并阻止程序员再次使用它(除非它被重新初始化)。在这种情况下, unwrap 有签名:

impl Option<T> {
fn unwrap(self) -> T { ...

也就是取Option value by value 并因此试图消耗它的所有权。因此,self.maybe_file.unwrap()正在尝试使用 maybe_file 中的数据这会留下 self指向部分无效数据(之后使用 maybe_file 字段是非法的)。编译器无法通过借用的引用强制执行此操作,因为它们可以指向任何地方,因此必须始终有效,因此移出是非法的。

幸运的是,可以避免这个问题: as_ref 方法创建一个 Option<&T>从一个&Option<T> as_mut 方法创建一个 Option<&mut T>从一个&mut Option<T> .结果 Option然后不再是引用,因此通过 unwrap 使用它是合法的:

let mut file = self.maybe_file.as_mut().unwrap();

这略有不同,因为 file类型为 &mut File而不是 File ,还好&mut File是其余代码所必需的。

完成这项工作的另一种方法是使用手动模式匹配:

match self.maybe_file {
Some(ref mut file) => println!(...),
None => panic!("error: file was missing")
}

这与 .as_mut().unwrap() 做的完全一样更明确地说:ref mut是创建一个直接指向 self.maybe_file 占用的内存的引用, 就像 as_mut .

关于rust - 无法从 &mut self 借用文件(错误消息 : cannot move out of borrowed content),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28034646/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com