0

I have a file called info.rs that contains a small test structure intended to represent some basic file information. The code below is learning code for using structs:

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
    }
}

According to documentation (and several examples!), the &self parameter used in the above functions refers to the calling structure, in this case the FileInfo struct. The intent is to allow my main.rs code to get access to the name and path:

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());
}

Unfortunately, compilation fails with the following messages:

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

This makes no sense, because code from https://doc.rust-lang.org/rust-by-example/fn/methods.html accesses the &self parameters in the same way that I am:

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;
    }
}

This code compiles, while mine does not. Why is this?

Can someone tell me what I am missing here?

Factor Three
  • 2,094
  • 5
  • 35
  • 51
  • 1
    That's not the code that produced those errors. You must have the code in a text editor. So copy and paste it (after reducing it to a minimal, but complete example). Don't type it from memory. – Benjamin Lindley Mar 09 '19 at 09:51
  • You're missing the return type in `get_name`. Also, if it's an internal structure and is not going to be exposed to the end user, making the fields public and dropping the getters altogether will be much easier. – Michail Mar 09 '19 at 11:16
  • 1
    Note also that it is not idiomatic to have a `get_` prefix in getter methods. ([C-GETTER](https://rust-lang-nursery.github.io/api-guidelines/naming.html#getter-names-follow-rust-convention-c-getter)) – E_net4 Mar 09 '19 at 11:52

2 Answers2

6

There's a few fundamentals you need to brush up on before getting frustrated with the language.

Artemiy above gave you the basic fix .. f64 implements Copy and as such, there's no ownership issues when you return them out of a function (they just get copied).

String however, does not implement Copy and therefore your code actually says "move the String values out of the methods and transfer ownership to the caller". The compiler doesn't let you do this because you've already borrowed the FileInfo struct by calling into the method.

The two ways given above (clone or return a reference) are below:

// clone it:
pub fn get_name(&self) -> String {
   self.name.clone() // <-- call `clone()` on the `String` instance to return a new copy out
}

// return a reference:
pub fn get_path(&self) -> &String {
    &self.path
}

If you're unsure why the above works, then you'll need to consult the source material again to understand references and the differences between things that implement Copy and those that don't.

You might also even benefit from understanding Deref coercion, because you can return a &str reference too:

// Deref coercion to return a `&str` reference
pub fn get_path(&self) -> &str {
    &self.path
}

Here it is running in the Playground

Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
  • 3
    You should emphatize that `&str` is **the** way to go – Boiethios Mar 09 '19 at 12:46
  • The book(s) I am reading in order to learn Rust did warn me about the little "gotchas" I could run into when dealing with borrowing and ownership. This looks like one of those. Ah, well: lesson learned... – Factor Three Mar 09 '19 at 14:57
3

The struct from Rust By Example contains only f64 fields. A f64 implements Copy trait, so the primitive has copy semantic instead of move semantic.

The struct in your case contains String fields, these implement only Clone trait. So you have to make a clone or return a reference.

Also you should see the difference between Copy and Clone and the list of Copy implementors

Artemij Rodionov
  • 1,721
  • 1
  • 17
  • 22