Borrow checker woes related to returning a reference (E0505 and E515)

I am stymied by a borrow checker error in my code (some kind of tokenizer):
I have an enum with String and &str variants, and want to turn the former variant into the latter. Is there any way to do this?

I tried the following

reduced code example
enum Token<'input> {
    Command(&'input str),
fn g(t: Token) -> Token {
    if let Token::FreeString(s) = t {
        Token::Command(&s) // this line causes the error
    } else {
fn main() {}

I got the following output.

Click for detail
Compiling playground v0.0.1 (/playground)
error[E0515]: cannot return value referencing local variable `s`
 --> src/
7 |         Token::Command(&s) // this line causes the error
  |         ^^^^^^^^^^^^^^^--^
  |         |              |
  |         |              `s` is borrowed here
  |         returns a value referencing data owned by the current function

Thinking about this more closely, I don't expect the code to compile any more --- but I expect there to be a way to make this compile. Any suggestions that work on this reduced example --- or do I need to restructure my original code?

This cannot be made to work without changing the definition of Token because the string you want the &str to point at is destroyed when g returns.

I recommend changing it to store a String in both cases.

Well, this compiles I guess:

fn g<'short, 'long>(t: &'short Token<'long>) -> Token<'short> {
    match t {
        Token::Command(cmd) => Token::Command(cmd),
        Token::FreeString(fs) => Token::Command(&fs),

The above works because the argument is a reference, so the owner of the free string stays alive. Still, I recommend changing it to store a String in both cases.

The thing is that you pass instance of "Token" to function "g" and it is owned by the function. So as soon as function ends the instance is dropped, but at the same time in the body you try to create a completly new instance of a "Token" that references data that is supposed to be dropped.

As an option:

fn g<'a>(t: &'a Token<'a>) -> Option<Token<'a>> {
    if let Token::FreeString(s) = t {
        Some(Token::Command(&s)) // this line causes the error
    } else {

Thanks for the help, everyone! You have convinced me that this design can indeed not work --- hence I need to (and will) restructure my code.

I could make both variants a String --- but in fact, this question arose in an attempt to make my code operate on string slices only. (Not for any specific need; I don't need that bit of performance either --- more out of curiosity if I could push my code this far.) However, I see now how to restructure my code so both variants are &str, and will do that.