-3

I have the following method to check if an String ID exists. If it doesn't, generate and then return it:

fn generate_id(&self) -> ID<'m> {
    let id = nanoid::generate(15);
    while self[&id].is_some() {
        id = nanoid::generate(15);
    };
    id
}

ID is a type alias: type ID<'id> = &'id String;

The return value needs to be &'m std::string::String but id is std::string::String.

I have tried doing:

let id: ID<'m> = nanoid::generate(15);

but then it gives the same error that the method is giving only for id.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Thermatix
  • 2,757
  • 21
  • 51
  • 1
    Please provide a [MCVE]. We don't know what the signature of `nanoid::generate` is for sure, or what the implementation of `self[]` is for the code presented. – Shepmaster Mar 30 '18 at 22:31

2 Answers2

2

Lifetimes are descriptive, not prescriptive. You don't set lifetimes; they are a consequence of the program you write.

You are trying to return a reference to a local variable. That is not valid, and there is no lifetime you can write to make it valid.

You have an X/Y problem. The real question is why you feel the need to return a reference.

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157
  • I agree with your points, but (presumably) OP solved their case because they answered, even though you and I agree it's impossible based on the information given. The question feels like it's unanswerable (or a dupe of something). – Shepmaster Mar 30 '18 at 22:39
  • I'd also have a minor quibble with the terminology "temporary". Temporary would be the `String` in `String::new().trim()`; this would just be a local variable. – Shepmaster Mar 30 '18 at 22:41
  • Good point about temporary. I still think my answer is important to other readers, simply because the OPs self-answer makes no sense. – Sebastian Redl Mar 31 '18 at 11:00
-3

You need to focus on where the variable is set, not where you actually return the variable.

In this case, change nanoid::generate(15); to &nanoid::generate(15);:

fn generate_id(&self) -> ID<'m> {
    let id: ID<'m> = &nanoid::generate(15);
    while self[&id].is_some() {
        id = &nanoid::generate(15);
    };
    id
}

This ensures that the initial type of the variable has the lifetime and supplying the value as a reference ensures that the variable has the correct lifetime.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Thermatix
  • 2,757
  • 21
  • 51
  • If `nanoid::generate` returns a `String`, as you say in the question, then this code cannot work because you [cannot return a reference to a value created in a function](https://stackoverflow.com/q/32682876/155423). – Shepmaster Mar 30 '18 at 22:33