2

I'm very new to Rust and to get more familiar with the language I started to write a little ToDo program.

I've made a Struct corresponding to a Task :

    struct Task {
        id: int,
        title: &'static str
    }

And I tried to implement some methods to get directly the formatted line to write in a file :

    impl Task {
        fn to_string (&self) -> String {
            let line = self.id.to_string() + "\t" + self.title;
            line
        }

        fn to_str (&self) -> &str {
            let line = self.to_string();
            let formatted_str : &str = line.as_slice();
            formatted_str
        }
    }

I get an error which says that 'line' does not live long enough. I've tried to add the lifetime information as it is explained here : http://rustbyexample.com/lifetime/fn.html but I've got the same error again.

Is there a solution to this situation ? I'm new to Rust so I probably missed something.

Thanks for your help !

2 Answers 2

4

This is fundamentally not possible. If you return a reference to something, that something must be stored somewhere. Ignoring task-local storage and statics, that means for a method that takes just &self that it must be stored in self.

You cannot return a reference to something that is owned by the method you are returning it from, for when you return the reference to it, it passes out of scope and is destroyed.

Let your caller call self.to_string().as_slice() themselves if they need a string slice rather than an owned string.


Incidentally, for best results you should be implementing std::fmt::Show;

use std::fmt;

impl fmt::Show for Task {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}\t{}", self.id, self.title)
    }
}

That gets you a .to_string() method automatically, and you can use your Task directly in format!("{}", task) and so forth.

Sign up to request clarification or add additional context in comments.

Comments

3

If we look at the rust documentation for str, we find a couple of insightful excerpts.

&str is the borrowed string type. This type of string can only be created from other strings, unless it is a static string

and

The actual representation of strings have direct mappings to vectors: &str is the same as &[u8].

This means that a non-static &str is represented as a pointer into the data owned by a vector of u8's (i.e. a String). The &str itself, however, does not own the u8's.

line, in your code above, is a local variable in the to_str function. This means that when to_str returns, line goes out of scope, and it destroys the data it owns: a bunch of u8's. formatted_str is borrowing the data owned by line. Thus, you cannot return formatted_str because the lifetime of the data it is borrowing ends when to_str returns. You may find this clearer in a simplified version of the code you provided.

I'm not experienced enough with rust myself to provide a solution to your problem, but hopefully this explains the error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.