0

OK, here's my MCVE, right off the bat.

fn do_something (string: &'static str) -> Result<&str, isize> {
    Ok(string)
}

fn main() {
    let place = Some("hello".to_string());
    match place {
        Some(input) => {
            let place = &input[..];
            let something = do_something(place);
        }
        _ => (),
    }
}

I can't seem to figure out a way in which to satisfy do_something. In my actual code, do_something is a library function, so I can't change it's signature.

- Thanks

Alexis Dumas
  • 1,299
  • 11
  • 30

1 Answers1

0

If you can't change the function's signature, then you either need to use a string literal to create a &'static str or leak memory.

i.e. either do this:

do_something("hello");

or this (bad idea, will probably break, only works on nightly):

let place = Some("hello".to_string());
if let Some(s) = place {
    do_something(unsafe { std::mem::transmute(s.into_boxed_str()) });
}
Adrian
  • 14,931
  • 9
  • 45
  • 70