Rust 如何将字符串转换为&';静态str

Rust 如何将字符串转换为&';静态str,rust,Rust,如何将字符串转换为&str?更具体地说,我想将其转换为具有静态寿命(&'static str)的str。更新为Rust 1.0 您无法从字符串中获取&'static str,因为字符串可能不会在程序的整个生命周期内都有效,这就是&'static生命周期的含义。您只能从中获取由Stringown life参数化的切片 要从字符串转到切片和str,可以使用切片语法: let s: String = "abcdefg".to_owned(); let s_slice: &str = &

如何将
字符串
转换为
&str
?更具体地说,我想将其转换为具有
静态
寿命(
&'static str
)的
str

更新为Rust 1.0

您无法从
字符串
中获取
&'static str
,因为
字符串
可能不会在程序的整个生命周期内都有效,这就是
&'static
生命周期的含义。您只能从中获取由
String
own life参数化的切片

要从
字符串
转到切片
和str
,可以使用切片语法:

let s: String = "abcdefg".to_owned();
let s_slice: &str = &s[..];  // take a full slice of the string
或者,您可以使用
String
实现
Deref
并执行显式重新加载的事实:

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

请注意,此模式对于
String
/
&str
不是唯一的-您可以将其用于通过
Deref
连接的每对类型,例如,使用模块中的
CString
/
CStr
OsString
/
OsStr
,或模块中的
PathBuf
/
路径

针对Rust 1.0更新

您无法从
字符串
中获取
&'static str
,因为
字符串
可能不会在程序的整个生命周期内都有效,这就是
&'static
生命周期的含义。您只能从中获取由
String
own life参数化的切片

要从
字符串
转到切片
和str
,可以使用切片语法:

let s: String = "abcdefg".to_owned();
let s_slice: &str = &s[..];  // take a full slice of the string
或者,您可以使用
String
实现
Deref
并执行显式重新加载的事实:

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

请注意,此模式对于
String
/
&str
不是唯一的-您可以将其用于通过
Deref
连接的每对类型,例如,使用模块中的
CString
/
CStr
OsString
/
OsStr
或模块中的
PathBuf
/
Path

您可以这样做,但它涉及泄漏
字符串的内存。这不是你应该轻易做的事。通过泄漏
字符串的内存
,我们保证永远不会释放内存(因此泄漏)。因此,对内部对象的任何引用都可以解释为具有
的静态生命周期

fn string_to_static_str(s: String) -> &'static str {
    Box::leak(s.into_boxed_str())
}

fn main() {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    let s: &'static str = string_to_static_str(s);
}

您可以这样做,但它涉及泄漏
字符串的内存。这不是你应该轻易做的事。通过泄漏
字符串的内存
,我们保证永远不会释放内存(因此泄漏)。因此,对内部对象的任何引用都可以解释为具有
的静态生命周期

fn string_to_static_str(s: String) -> &'static str {
    Box::leak(s.into_boxed_str())
}

fn main() {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    let s: &'static str = string_to_static_str(s);
}

从Rust版本1.26开始,可以将
字符串
转换为
&'static str
,而无需使用
不安全的
代码:

fn string_to_static_str(s: String) -> &'static str {
    Box::leak(s.into_boxed_str())
}
这会将
字符串
实例转换为装箱的
str
,并立即将其泄漏。这将释放字符串当前可能占用的所有多余容量


请注意,几乎总是有比泄漏物体更可取的解决方案,例如,如果您想在线程之间共享状态,可以使用
横梁
板条箱。

从Rust版本1.26开始,可以将
字符串
转换为
静态str
,而无需使用
不安全的
代码:

fn string_to_static_str(s: String) -> &'static str {
    Box::leak(s.into_boxed_str())
}
这会将
字符串
实例转换为装箱的
str
,并立即将其泄漏。这将释放字符串当前可能占用的所有多余容量


请注意,几乎总是有比泄漏对象更可取的解决方案,例如,如果您想在线程之间共享状态,请使用
横梁
板条箱。

TL;DR:您可以从本身具有
静态寿命的
字符串中获取
&'static str

fn string_to_static_str(s: String) -> &'static str {
    Box::leak(s.into_boxed_str())
}

fn main() {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    let s: &'static str = string_to_static_str(s);
}
尽管其他答案是正确的,也是最有用的,但有一个(不太有用的)边缘案例,您确实可以将
字符串
转换为
&'static str

引用的生存期必须始终短于或等于被引用对象的生存期。也就是说,被引用对象的寿命必须比引用对象长(或等于引用对象的寿命)。由于静态
意味着程序的整个生命周期,因此不存在更长的生命周期。但同样的寿命就足够了。因此,如果
字符串
的生存期为
'static
,则可以从中获得
&'static str
引用

const-fn
功能被释放时,创建
String
类型的
static
理论上已成为可能,Rust 1.31。不幸的是,当前唯一返回
字符串的const函数是
String::new()
,它仍然位于功能门之后(因此现在需要每晚运行)

因此,下面的代码进行了所需的转换(使用夜间)…实际上没有实际用途,除了完整地显示在这种边缘情况下是可能的

#![功能(常量字符串新)]
静态MY_STRING:STRING=STRING::new();
fn做某事(&'static str){
// ...
}
fn main(){
做点什么(我的字符串);
}

TL;DR:您可以从本身具有
静态寿命的
字符串中获取
&'static str

fn string_to_static_str(s: String) -> &'static str {
    Box::leak(s.into_boxed_str())
}

fn main() {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    let s: &'static str = string_to_static_str(s);
}
尽管其他答案是正确的,也是最有用的,但有一个(不太有用的)边缘案例,您确实可以将
字符串
转换为
&'static str

引用的生存期必须始终短于或等于被引用对象的生存期。也就是说,被引用对象的寿命必须比引用对象长(或等于引用对象的寿命)。由于静态
意味着程序的整个生命周期,因此不存在更长的生命周期。但同样的寿命就足够了。所以如果