У меня есть файл с именем info.rs, который содержит небольшую тестовую структуру, предназначенную для представления некоторой базовой информации о файле. Код ниже является обучающим кодом для использования структур:
pub struct FileInfo {
name: String,
path: String,
}
impl FileInfo {
pub fn new(aname: String,apath: String) {
FileInfo {
name: aname,
path: apath
}
}
pub fn get_name(&self) {
self.name
}
pub fn get_path(&self) -> String {
self.path
}
}
Согласно документации (и нескольким примерам!), Параметр & self, используемый в вышеприведенных функциях, относится к вызывающей структуре, в данном случае к структуре FileInfo. Цель состоит в том, чтобы мой код main.rs мог получить доступ к имени и пути:
mod info;
use info::FileInfo;
fn main() {
println!("Listing files in current directory.");
let fdat = FileInfo::new(String::from("File.txt".),String::from("./File.txt"));
println!("Name: {}",fdat.get_name());
println!("Path: {}",fdat.get_path());
}
К сожалению, компиляция завершается со следующими сообщениями:
error[E0507]: cannot move out of borrowed content
--> src\info.rs:79:9
|
79 | self.name
| ^^^^^^^^^ cannot move out of borrowed content
error[E0507]: cannot move out of borrowed content
--> src\info.rs:83:9
|
83 | self.path
| ^^^^^^^^^ cannot move out of borrowed content
error: aborting due to 2 previous errors
Это не имеет смысла, потому что код из https://doc.rust -lang.org / rust-by-example / fn / method.html обращается к параметрам & self так же, как и я:
struct Point {
x: f64,
y: f64,
}
// Implementation block, all `Point` methods go in here
impl Point {
// This is a static method
// Static methods don't need to be called by an instance
// These methods are generally used as constructors
fn origin() -> Point {
Point { x: 0.0, y: 0.0 }
}
// Another static method, taking two arguments:
fn new(x: f64, y: f64) -> Point {
Point { x: x, y: y }
}
}
struct Rectangle {
p1: Point,
p2: Point,
}
impl Rectangle {
// This is an instance method
// `&self` is sugar for `self: &Self`, where `Self` is the type of the
// caller object. In this case `Self` = `Rectangle`
fn area(&self) -> f64 {
// `self` gives access to the struct fields via the dot operator
let Point { x: x1, y: y1 } = self.p1;
let Point { x: x2, y: y2 } = self.p2;
// `abs` is a `f64` method that returns the absolute value of the
// caller
((x1 - x2) * (y1 - y2)).abs()
}
fn perimeter(&self) -> f64 {
let Point { x: x1, y: y1 } = self.p1;
let Point { x: x2, y: y2 } = self.p2;
2.0 * ((x1 - x2).abs() + (y1 - y2).abs())
}
// This method requires the caller object to be mutable
// `&mut self` desugars to `self: &mut Self`
fn translate(&mut self, x: f64, y: f64) {
self.p1.x += x;
self.p2.x += x;
self.p1.y += y;
self.p2.y += y;
}
}
Этот код компилируется, а мой - нет. Почему это?
Может кто-нибудь сказать мне, что мне здесь не хватает?