有没有办法在需要字符串时避免调用.to_string()
?例如:
fn func1(aaa: String) -> ....
而不是
func1("fdsfdsfd".to_string())
我可以像这样做吗:
func1(s"fdsfdsfd")
简而言之:
从Rust 1.9版本开始,str::to_string
,str::to_owned
,String::from
,str::into
的性能特征相同。使用任何你喜欢的方法。
&str
)转换为拥有的字符串(String
)的方法是使用ToString::to_string
。这适用于实现了Display
的任何类型,包括字符串切片、整数、IP地址、路径、错误等等。to_string
的str
实现利用了格式化基础设施。虽然它能工作,但过于繁琐而且不是最高效的路径。
一种更轻量级的解决方案是使用 ToOwned::to_owned
,它适用于具有“借用”和“拥有”对的类型。这个方法的实现方式非常高效。
另一个轻量级的解决方案是使用 Into::into
,它利用了 From::from
。这个方法也被高效地实现。
&str
,如 thirtythreeforty answered所述。然后你需要进行零分配内存,这是最好的结果。into
——它只有4个字母 ^_^。在回答Stack Overflow上的问题时,我会使用to_owned
,因为这样更容易理解正在发生的事情。str::to_string()
方法是从 &'static str
(字符串字面量)创建 String
的规范方式。即使您不喜欢它的原因,我也喜欢它:它有点冗长。因为它涉及堆分配,所以在这种情况下调用它之前应该三思而后行。另请注意,自从 Rust 获得了 impl specialization 以来,str::to_string
不比 str::to_owned
或其类似物慢。func1
的函数,它可以轻松地传递任何字符串,无论是&str
还是String
。因为String
会Deref到&str
,所以你可以让func1
接受&str
,从而完全避免字符串分配。请参见此示例(playground):fn func1(s: &str) {
println!("{}", s);
}
fn main() {
let allocated_string: String = "owned string".to_string();
func1("static string");
func1(&allocated_string);
}
I now strongly prefer
to_owned()
for string literals over either ofto_string()
orinto()
.What is the difference between
String
and&str
? An unsatisfactory answer is “one is a string and the other is not a string” because obviously both are strings. Taking something that is a string and converting it to a string usingto_string()
seems like it misses the point of why we are doing this in the first place, and more importantly misses the opportunity to document this to our readers.The difference between String and &str is that one is owned and one is not owned. Using
to_owned()
fully captures the reason that a conversion is required at a particular spot in our code.
struct Wrapper { s: String } // I have a string and I need a string. Why am I doing this again? Wrapper { s: "s".to_string() } // I have a borrowed string but I need it to be owned. Wrapper { s: "s".to_owned() }
如果你在脑海中将
to_string
读作to_String
,那就不行了。
to_string
。但是通过专业化,就不会这样了。 - Steve Klabnikto_string
绝对是惯用的选择 - 只是目前不是性能最佳的选择。我六个月前曾发表评论,其中替换to_string
使我的代码执行时间缩短了87%。 - Shepmasterto_owned()
完全捕获了我们代码中特定位置需要进行转换的原因。 - rofrol