is there way avoid calling .to_string() when need string? example:
fn func1(aaa: string) -> .... and instead of
func1("fdsfdsfd".to_string()) can this:
func1(s"fdsfdsfd")
tl;dr:
as of rust 1.9, str::to_string, str::to_owned, string::from, str::into have same performance characteristics. use whichever prefer.
the obvious , idiomatic way convert string slice (&str) owned string (string) use tostring::to_string. works type implements display. includes string slices, integers, ip addresses, paths, errors, , on.
before rust 1.9, str implementation of to_string leveraged formatting infrastructure. while worked, overkill , not performant path.
a lighter solution use toowned::to_owned, implemented types have "borrowed" , "owned" pair. implemented in efficient manner.
another lightweight solution use into::into leverages from::from. implemented efficiently.
for specific case, best thing accept &str, thirtythreeforty answered. need zero allocations, best outcome.
in general, use into if need make allocated string — it's 4 letters long ^_^. when answering questions on stack overflow, i'll use to_owned it's more obvious happening.
Comments
Post a Comment