【发布时间】:2018-07-06 11:16:09
【问题描述】:
我不明白这个借用检查器错误:
pub fn wait_for_device(&mut self) -> RoxResult<hidapi::HidDevice> {
let mut device = self.open_device();
let start = time::Instant::now();
while device.is_err() {
device = self.open_device();
if start.elapsed().as_secs() > 30 {
return Err("Can't reconnect to device".to_owned());
}
}
Ok(device.expect("Out of while so we should have a device"))
}
pub fn open_device(&mut self) -> RoxResult<hidapi::HidDevice> {
let device_info = &self.list[0];
if let Ok(device) = self.api.open(device_info.vendor_id, device_info.product_id) {
self.current_device = Some(device_info.clone());
Ok(device)
} else {
Err(format!(
"Error opening device vip: {:x} pid: {:x}",
device_info.vendor_id, device_info.product_id
))
}
}
error[E0499]: cannot borrow `*self` as mutable more than once at a time
--> src\usb.rs:64:22
|
61 | let mut device = self.open_device();
| ---- first mutable borrow occurs here
...
64 | device = self.open_device();
| ^^^^ second mutable borrow occurs here
...
70 | }
| - first borrow ends here
我以为我的第一次借款将在open_device 结束时结束,但似乎我错了。为什么?
【问题讨论】:
-
请查看如何创建minimal reproducible example,然后查看edit 您的问题以包含它。我们无法分辨代码中存在哪些 crate、类型、特征、字段等。理想情况下,在Rust Playground 上生成可以重现您的错误的内容。还有Rust-specific MCVE tips。
标签: rust