我不明白这个错误cannot move out of borrowed content.我收到了很多次,我总是解决它,但我从来没有理解为什么.
例如:
for line in self.xslg_file.iter() {
self.buffer.clear();
for current_char in line.into_bytes().iter() {
self.buffer.push(*current_char as char);
}
println!("{}", line);
}
Run Code Online (Sandbox Code Playgroud)
产生错误:
error[E0507]: cannot move out of borrowed content
--> src/main.rs:31:33
|
31 | for current_char in line.into_bytes().iter() {
| ^^^^ cannot move out of borrowed content
Run Code Online (Sandbox Code Playgroud)
我通过克隆解决了这个问题line:
error[E0507]: cannot move out of `*line` which is behind a shared reference
--> src/main.rs:31:33
|
31 | for current_char in line.into_bytes().iter() {
| ^^^^ …Run Code Online (Sandbox Code Playgroud) 这个函数的实现是什么:
fn unbox<T>(value: Box<T>) -> T {
// ???
}
Run Code Online (Sandbox Code Playgroud)
文档中唯一看起来像我想要的功能Box::into_raw.以下将进行类型检查:
fn unbox<T>(value: Box<T>) -> T {
*value.into_raw()
}
Run Code Online (Sandbox Code Playgroud)
这给出了错误error[E0133]: dereference of raw pointer requires unsafe function or block.将其包装在一个unsafe { ... }块中可以修复它.
fn unbox<T>(value: Box<T>) -> T {
unsafe { *value.into_raw() }
}
Run Code Online (Sandbox Code Playgroud)
这是正确的实施吗?如果是这样,为什么它不安全?这是什么意思?
也许这个问题显示了我对如何Box实际工作的一般不确定性.
我有一个带字段的结构:
struct A {
field: SomeType,
}
Run Code Online (Sandbox Code Playgroud)
给定a &mut A,如何移动值field并交换新值?
fn foo(a: &mut A) {
let mut my_local_var = a.field;
a.field = SomeType::new();
// ...
// do things with my_local_var
// some operations may modify the NEW field's value as well.
}
Run Code Online (Sandbox Code Playgroud)
最终目标相当于一项get_and_set()行动.在这种情况下,我并不担心并发性.
我正在编写一个非常简单的getter/setting模型,我想在Rust中使用struct和出于简单的原因使用和impl.
struct Person {
firstName: String,
lastName: String,
}
impl Person {
fn get_first_name(&mut self) -> String { return self.firstName; }
fn get_last_name(&mut self) -> String { return self.lastName; }
fn set_first_name(&mut self, x: String) { self.firstName = x; }
fn set_last_name(&mut self, x: String) { self.lastName = x; }
fn default() -> Person {
Person {firstName: "".to_string(), lastName: "".to_string()}
}
}
fn main() {
let mut my_person : Person = Person{ ..Person::default() };
my_person.set_first_name("John".to_string());
my_person.set_last_name("Doe".to_string()); …Run Code Online (Sandbox Code Playgroud) 我试图在后台运行一个线程,然后更改 AtomicBool 来要求线程停止。为了确保线程正确停止,我想调用joinJoinHandle 的方法。
thread::sleep_ms如果我在设置 AtomicBool 后等待 () 一段时间,而不是调用 join ,线程就会正确关闭。但为了确保这一点,我想使用 join。或者有没有更好的方法来确保线程正确关闭?
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread;
use std::thread::JoinHandle;
struct MyInner { s: String }
struct My {
inner: Arc<Mutex<MyInner>>,
close_thread: Arc<AtomicBool>,
my_thread: JoinHandle<()>
}
impl MyInner {
fn new(s: String) -> MyInner {
MyInner {
s: s
}
}
}
impl My {
fn new(s: String) -> My {
My {
inner: Arc::new(Mutex::new(MyInner::new(s))),
close_thread: Arc::new(AtomicBool::new(false)),
my_thread: thread::spawn(move || {})
}
}
fn …Run Code Online (Sandbox Code Playgroud) 我试图在Rust中创建一个Disjoint-Set数据结构。相关代码为:
pub struct Set<'a, T: 'a> {
rank: u32,
value: T,
parent: Option<&'a mut Set<'a, T>>,
}
impl<'a, T> Set<'a, T> {
pub fn find(&'a mut self) -> &'a mut Set<'a, T> {
match self.parent {
None => self,
Some(mut p) => {
self.parent = Some(p.find());
self.parent.unwrap()
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
pub struct Set<'a, T: 'a> {
rank: u32,
value: T,
parent: Option<&'a mut Set<'a, T>>,
}
impl<'a, T> Set<'a, T> {
pub fn find(&'a mut self) -> &'a …Run Code Online (Sandbox Code Playgroud) I'm looking for something roughly like this take, but atomic:
impl<T: Clone> for Arc<T> {
fn take(mut self) -> T {
Arc::make_mut(&mut self);
Arc::try_unwrap(self).unwrap()
}
}
Run Code Online (Sandbox Code Playgroud)
In other words, I want Arc::make_mut which returns the value itself, rather than a mutable reference.
我试图将变量的所有权赋予循环中的函数,并且我有自己的布尔值以确保它只发生一次,但是编译器告诉我在前一次迭代中移动了值.
这是一个例子:
fn take_ownership(a: String) {
println!("{}", a);
}
fn main() {
let mut a = true;
let hello = "Hello".to_string();
for _ in 0..5 {
if a {
a = false;
take_ownership(hello);
}
}
}
Run Code Online (Sandbox Code Playgroud)
使用此代码,编译器告诉我:
error[E0382]: use of moved value: `hello`
--> src/main.rs:12:28
|
12 | take_ownership(hello);
| ^^^^^ value moved here in previous iteration of loop
Run Code Online (Sandbox Code Playgroud)
有没有办法告诉编译器"没关系,我会处理它"?我不想使用references(&).