Skip to main content

How to Convert a &str to a String in Rust

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Rust has two main string types. &str is a borrowed view of text, such as a string literal, and String is text your code owns. When a variable, a struct field or a function needs a String, convert the &str with to_owned(), to_string() or String::from().

Syntax​

Syntax
let <owned>: String = <text>.to_owned();
let <owned>: String = <text>.to_string();
let <owned>: String = String::from(<text>);
let <owned>: String = <text>.into();
PartWhat it does
<text>A &str, such as "Ada" or a &str variable.
.to_owned()Makes an owned copy of borrowed data: for a &str, a String.
.to_string()Turns the value into a String.
String::from(<text>)Creates a String from the text.
.into()Converts into the type the variable declares, here String.

The standard library's documentation for ToOwned::to_owned: "Creates owned data from borrowed data, usually by cloning." Its own example is let ss: String = s.to_owned();. For String::from, the String documentation says: "Converts a &str into a String. The result is allocated on the heap."

The Example​

The example converts the text "Ada" three ways and prints the three String values side by side.

Prerequisites​

  • Rust, installed with rustup, which comes with Cargo. This guide uses Rust 1.100.0.

Step 1: Create a Project​

Terminal
cargo new demo
cd demo
Output
Creating binary (application) `demo` package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

cargo new demo creates a folder named demo with a Cargo.toml file, which describes the project, and src/main.rs, which holds the code. cd demo moves into it.

Step 2: Write the Code​

Replace the contents of src/main.rs with:

src/main.rs, where text is the str Ada and a, b and c are Strings made with text.to_owned(), text.to_string() and String::from(text), printed on one line.

src/main.rs
fn main() {
let text: &str = "Ada";

let a: String = text.to_owned();
let b: String = text.to_string();
let c: String = String::from(text);

println!("{a} {b} {c}");
}

The type annotations, &str and String, are only there to show the types: the code works without them.

Step 3: Run It​

Terminal
cargo run -q
Output
Ada Ada Ada

A zsh terminal where cargo run -q prints Ada Ada Ada.

cargo run compiles the program and runs it, and -q hides Cargo's own status lines. All three conversions produced the same String. A fourth, let d: String = text.into();, prints Ada too.

When the Compiler Asks for It​

Assigning a &str where a String is expected stops the build, and the compiler suggests a conversion:

src/main.rs
fn main() {
let text: &str = "Ada";
let name: String = text;
println!("{name}");
}
Output
error[E0308]: mismatched types
--> src/main.rs:3:24
|
3 | let name: String = text;
| ------ ^^^^ expected `String`, found `&str`
| |
| expected due to this
|
help: try using a conversion method
|
3 | let name: String = text.to_string();
| ++++++++++++

For more information about this error, try `rustc --explain E0308`.
error: could not compile `demo` (bin "demo") due to 1 previous error

Common Mistakes​

MistakeWhat happensDo this instead
Assigning a &str to a String variable or fielderror[E0308]: mismatched types, expected `String`, found `&str` Convert it with to_owned(), to_string() or String::from()
Using .into() without a declared target typeerror[E0283]: type annotations needed: the compiler can't tell what to convert intoDeclare the type, as in let d: String = text.into();
Converting a &str you only readAn extra copy you don't needKeep the &str when nothing has to own the text

Troubleshooting​

SymptomLikely causeFix
expected `String`, found `&str` A &str where a String is neededAdd .to_string(), as the compiler's help shows
help: try using a conversion methodThe same mismatchApply the suggestion

FAQ​

Which one should I use?​

They give the same String, so it's a matter of style. to_owned() says "make an owned copy", String::from() reads naturally when you build a String from a literal, and to_string() is what the compiler suggests.

Why does a struct need String and not &str?​

A struct that holds a &str only borrows the text, so it needs a lifetime saying how long the text lives. A field written as name: &str without one stops the build with error[E0106]: missing lifetime specifier. A String field owns its text, which keeps the struct simple.

Conclusion​

to_owned(), to_string(), String::from() and .into() all turn a &str into an owned String. Convert when something needs to own the text, and keep the &str when you only read it.

Additional Resources​

  • ToOwned, in the standard library documentation.
  • String, in the standard library documentation.