A
A
Andrey2020-11-24 13:49:15
Rust
Andrey, 2020-11-24 13:49:15

How to return a value from a struct field in a method?

Hello, maybe the question sounds wildly stupid, but I can not implement such a simple thing as calling two methods on an object.

There is a simple structure with two methods:

pub struct MimeType {
    pub extension: String,
    pub charset: String,
}

impl MimeType {
    pub fn new(extension: String, charset: String) -> MimeType {
        return MimeType {
            extension,
            charset,
        };
    }

    pub fn extension(self) -> String {
        return self.extension;
    }

    pub fn charset(self) -> String {
        return self.charset;
    }
}


I'm trying to write tests for them:

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn is() {
        let m = MimeType::new(
            "test".to_owned(),
            "UTF-8".to_owned()
        );

        assert_eq!("UTF-8", m.charset());
        assert_eq!("UTF-8", m.extension());
    }
}


But I get in response:

error[E0382]: use of moved value: `m`
  --> src\mime\mod.rs:35:29
   |
29 |         let m = MimeType::new(
   |             - move occurs because `m` has type `mime::MimeType`, which does not implement the `Copy` trait
...
34 |         assert_eq!("UTF-8", m.charset());
   |                               --------- `m` moved due to this method call
35 |         assert_eq!("UTF-8", m.extension());
   |                             ^ value used here after move
   |
note: this function consumes the receiver `self` by taking ownership of it, which moves `m`
  --> src\mime\mod.rs:18:20
   |
18 |     pub fn charset(self) -> String {
   |                    ^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0382`.
error: aborting due to previous error

For more information about this error, try `rustc --explain E0382`.
error: could not compile `r-mime`

To learn more, run the command again with --verbose.
warning: build failed, waiting for other jobs to finish...
error: build failed


I also tried to implement the `Copy` trait (not the fact that I did it right), only other errors appeared related to the same ...

What and how to, push pliz :)

ZY . I'm just learning to grow.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily Bannikov, 2020-11-24
@zikwall

try to write

pub struct MimeType {
    extension: String,
    charset: String,
}

impl MimeType {
    pub fn new(extension: String, charset: String) -> MimeType {
        return MimeType { extension, charset };
    }

    pub fn extension(&self) -> &String {
        return &self.extension;
    }

    pub fn charset(&self) -> &String {
        return &self.charset;
    }
}

the string is owned by MimeType, so you can't just take it - you need to either take a link or clone it.
Also pay attention to the telegram community chats - it is much easier and faster to get an answer to a simple question:
• https://t.me/rust_beginners_ru
• https://t.me/rustlang_ru

D
DarkEld3r, 2020-12-03
@DarkEld3r

I will add that it is better to give not &String, a &str.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question