Avatar ·

Comment convertir un String en &'static str en Rust ?

Bonjour à tous, je viens de commencer à apprendre Rust et j'ai déjà rencontré un problème lié aux durées de vie. J'ai une fonction qui retourne un &'static str, mais je reçois un String à partir de l'entrée utilisateur (via stdin) ou d'une autre opération dynamique. Comment puis-je convertir un String ordinaire en &'static str ? Voici un exemple de code qui ne compile pas : ```rust fn get_static_string(input: String) -> &'static str { &input } fn main() { let my_string = String::from("hello"); let static_str = get_static_string(my_string); println!("{}", static_str); } ``` Le compilateur affiche l'erreur : `cannot return reference to local variable 'input'`. Je comprends que cela est dû au fait que `input` sera détruit après la sortie de la fonction, mais j'ai besoin de retourner exactement un &'static str.