Is there any way to avoid calling .to_string()
when I need a string? For example:
fn func1(aaa: String) -> ....
And instead of
func1("fdsfdsfd".to_string())
can I do something like this:
func1(s"fdsfdsfd")
We can convert numbers to strings through using the str() method. We'll pass either a number or a variable into the parentheses of the method and then that numeric value will be converted into a string value. The quotes around the number 12 signify that the number is no longer an integer but is now a string value.
using printf() If we want to do a string output in C stored in memory and we want to output it as it is, then we can use the printf() function. This function, like scanf() uses the access specifier %s to output strings. The complete syntax for this method is: printf("%s", char *s);
TL;DR:
As of Rust 1.9, str::to_string
, str::to_owned
, String::from
, str::into
all have the same performance characteristics. Use whichever you prefer.
The most obvious and idiomatic way to convert a string slice (&str
) to an owned string (String
) is to use ToString::to_string
. This works for any type that implements Display
. This includes string slices, but also integers, IP addresses, paths, errors, and so on.
Before Rust 1.9, the str
implementation of to_string
leveraged the formatting infrastructure. While it worked, it was overkill and not the most performant path.
A lighter solution was to use ToOwned::to_owned
, which is implemented for types that have a "borrowed" and an "owned" pair. It is implemented in an efficient manner.
Another lightweight solution is to use Into::into
which leverages From::from
. This is also implemented efficiently.
For your specific case, the best thing to do is to accept a &str
, as thirtythreeforty answered. Then you need to do zero allocations, which is the best outcome.
In general, I will probably use into
if I need to make an allocated string — it's only 4 letters long ^_^. When answering questions on Stack Overflow, I'll use to_owned
as it's much more obvious what is happening.
No, the str::to_string()
method is the canonical way of creating a String
from an &'static str
(a string literal). I even like it for the reason you dislike it: it's a little verbose. Because it involves a heap allocation, you should think twice before invoking it in cases such as these. Also note that since Rust gained impl specialization, str::to_string
is no slower than str::to_owned
or its ilk.
However, what you really want here is a func1
that can easily be passed any string, be it a &str
or a String
. Because a String
will Deref to a &str
, you can have func1
accept an &str
, thereby avoiding the String allocation altogether. See this example (playground):
fn func1(s: &str) {
println!("{}", s);
}
fn main() {
let allocated_string: String = "owned string".to_string();
func1("static string");
func1(&allocated_string);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With