无法调用我的对象的两个方法,因为我 "use of moved value: `self`"
Can't call to two methods of my object because I "use of moved value: `self`"
我是 Rust 和一般低级编程的新手。我试图了解所有权以及内存如何与 Rust 一起工作。
但我不明白如何在我的对象的主要方法中调用多个 setter。
#[derive(Default, Clone)]
pub struct Metric {
loads: (f32, f32, f32),
memory: (String, String),
}
// Main method
pub fn get_metrics(self) {
let sys = System::new();
match sys.load_average() {
Ok(loadavg) => self.set_loads(loadavg.one, loadavg.five, loadavg.fifteen),
Err(x) => println!("\nLoad average: error: {}", x),
}
match sys.memory() {
Ok(mem) => self.set_memory(
saturating_sub_bytes(mem.total, mem.free).to_string(),
mem.total.to_string(),
),
Err(x) => println!("\nMemory: error: {}", x),
}
}
// Setters
fn set_loads(mut self, one: f32, five: f32, fifteen: f32) {
self.loads = (one, five, fifteen);
println!(
"\nLoad average {} {} {}",
self.loads.0, self.loads.1, self.loads.2
);
self;
}
fn set_memory(mut self, used: String, total: String) {
self.memory = (used, total);
println!("\nLoad average {} {}", self.memory.0, self.memory.1);
}
我知道我不能移动两次变量,但是如果我们不想一次性设置道具,在上下文中我们如何才能移动一次..
谢谢大家。
您正在移动您的对象,您可能只想对它使用引用,不可变的&self
或可变的&mut self
个:
// Getters
pub fn get_metrics(&mut self) {
let sys = System::new();
match sys.load_average() {
Ok(loadavg) => self.set_loads(loadavg.one, loadavg.five, loadavg.fifteen),
Err(x) => println!("\nLoad average: error: {}", x),
}
match sys.memory() {
Ok(mem) => self.set_memory(
saturating_sub_bytes(mem.total, mem.free).to_string(),
mem.total.to_string(),
),
Err(x) => println!("\nMemory: error: {}", x),
}
}
// Setters
fn set_loads(&mut self, one: f32, five: f32, fifteen: f32) {
self.loads = (one, five, fifteen);
println!(
"\nLoad average {} {} {}",
self.loads.0, self.loads.1, self.loads.2
);
}
fn set_memory(&mut self, used: String, total: String) {
self.memory = (used, total);
println!("\nLoad average {} {}", self.memory.0, self.memory.1);
}
我是 Rust 和一般低级编程的新手。我试图了解所有权以及内存如何与 Rust 一起工作。
但我不明白如何在我的对象的主要方法中调用多个 setter。
#[derive(Default, Clone)]
pub struct Metric {
loads: (f32, f32, f32),
memory: (String, String),
}
// Main method
pub fn get_metrics(self) {
let sys = System::new();
match sys.load_average() {
Ok(loadavg) => self.set_loads(loadavg.one, loadavg.five, loadavg.fifteen),
Err(x) => println!("\nLoad average: error: {}", x),
}
match sys.memory() {
Ok(mem) => self.set_memory(
saturating_sub_bytes(mem.total, mem.free).to_string(),
mem.total.to_string(),
),
Err(x) => println!("\nMemory: error: {}", x),
}
}
// Setters
fn set_loads(mut self, one: f32, five: f32, fifteen: f32) {
self.loads = (one, five, fifteen);
println!(
"\nLoad average {} {} {}",
self.loads.0, self.loads.1, self.loads.2
);
self;
}
fn set_memory(mut self, used: String, total: String) {
self.memory = (used, total);
println!("\nLoad average {} {}", self.memory.0, self.memory.1);
}
我知道我不能移动两次变量,但是如果我们不想一次性设置道具,在上下文中我们如何才能移动一次..
谢谢大家。
您正在移动您的对象,您可能只想对它使用引用,不可变的&self
或可变的&mut self
个:
// Getters
pub fn get_metrics(&mut self) {
let sys = System::new();
match sys.load_average() {
Ok(loadavg) => self.set_loads(loadavg.one, loadavg.five, loadavg.fifteen),
Err(x) => println!("\nLoad average: error: {}", x),
}
match sys.memory() {
Ok(mem) => self.set_memory(
saturating_sub_bytes(mem.total, mem.free).to_string(),
mem.total.to_string(),
),
Err(x) => println!("\nMemory: error: {}", x),
}
}
// Setters
fn set_loads(&mut self, one: f32, five: f32, fifteen: f32) {
self.loads = (one, five, fifteen);
println!(
"\nLoad average {} {} {}",
self.loads.0, self.loads.1, self.loads.2
);
}
fn set_memory(&mut self, used: String, total: String) {
self.memory = (used, total);
println!("\nLoad average {} {}", self.memory.0, self.memory.1);
}