How to convert a String into a &'static str

匿名 (未验证) 提交于 2019-12-03 02:49:01

问题:

How do I convert a String into a &str? More specifically, I would like to convert it into a str with the static lifetime (&'static str).

回答1:

Updated for Rust 1.0

You cannot obtain &'static str from a String because Strings do not live for the entire life of your program, and that's what &'static lifetime means. You can only get a slice parameterized by String own lifetime from it.

To go from a String to a slice &'a str you can use slicing syntax:

let s: String = "abcdefg".to_owned(); let s_slice: &str = &s[..];  // take a full slice of the string 

Alternatively, you can use the fact that String implements Deref and perform an explicit reborrowing:

let s_slice: &str = &*s;  // s  : String                            // *s : str (via Deref)                           // &*s: &str 

There is even another way which allows for even more concise syntax but it can only be used if the compiler is able to determine the desired target type (e.g. in function arguments or explicitly typed variable bindings). It is called deref coercion and it allows using just & operator, and the compiler will automatically insert an appropriate amount of *s based on the context:

let s_slice: &str = &s;  // okay  fn take_name(name: &str) { ... } take_name(&s);           // okay as well  let not_correct = &s;    // this will give &String, not &str,                          // because the compiler does not know                          // that you want a &str 

Note that this pattern is not unique for String/&str - you can use it with every pair of types which are connected through Deref, for example, with CString/CStr and OsString/OsStr from std::ffi module or PathBuf/Path from std::path module.



回答2:

You can do it, but it involves leaking the memory of the String. This is not something you should do lightly. By leaking the memory of the String, we guarantee that the memory will never be freed (thus the leak). Therefore, any references to the inner object can be interpreted as having the 'static lifetime.

use std::mem;  fn string_to_static_str(s: String) -> &'static str {     unsafe {         let ret = mem::transmute(&s as &str);         mem::forget(s);         ret     } }  fn main() {     let mut s = String::new();     std::io::stdin().read_line(&mut s).unwrap();     let s: &'static str = string_to_static_str(s); } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!