error[E0599]: no method named push_str found for reference &str
The method you called doesn’t exist for that type, at least not where Rust is looking. Either it belongs to a different type, or it comes from a trait you haven’t imported.
Quick fix: Check the type in the message: use the type that has the method (String instead of &str), or add the use line for the trait.
Code that triggers E0599
fn main() {
let greeting = "Hello";
greeting.push_str(", Ferris");
println!("{greeting}");
}What the compiler says
error[E0599]: no method named `push_str` found for reference `&str` in the current scope
--> src/main.rs:3:14
|
3 | greeting.push_str(", Ferris");
| ^^^^^^^^ method not found in `&str`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0599`.Why Rust raises E0599
greeting is a &str, a read-only borrowed string slice. push_str is a method of String, the growable owned string type, so it isn’t available on &str. The message names the type Rust actually saw, &str, which is often enough to spot the mix-up.
The second common cause is a method that does exist but comes from a trait that isn’t in scope. Trait methods only work after you import the trait with use. Here’s write! into a String without the import (output shortened):
fn main() {
let mut report = String::new();
write!(report, "{} errors", 3).unwrap();
println!("{report}");
}error[E0599]: cannot write into `String`
--> src/main.rs:3:12
|
3 | write!(report, "{} errors", 3).unwrap();
| ^^^^^^
|
note: must implement `io::Write`, `fmt::Write`, or have a `write_fmt` method
= help: items from traits can only be used if the trait is in scope
help: trait `Write` which provides `write_fmt` is implemented but not in scope; perhaps you want to import it
|
1 + use std::fmt::Write;
|How to fix E0599
1. Use the type that has the method
To append to text you need to own it: create a String and declare it mut.
fn main() {
let mut greeting = String::from("Hello");
greeting.push_str(", Ferris");
println!("{greeting}");
}2. Import the trait
When the compiler says a trait is implemented but not in scope, add the use line it suggests. Here write! into a String needs std::fmt::Write.
use std::fmt::Write;
fn main() {
let mut report = String::new();
write!(report, "{} errors", 3).unwrap();
println!("{report}");
}Related errors
Learn the concept behind it
The The Slice Type lesson explains the rules behind E0599 with diagrams and more examples. For the official reference, run rustc --explain E0599 or read the E0599 entry in the Rust error index .
Hit a different error? Paste it into the Rust error lookup to find its fix.