rust-lang / rust-lang/rust

Confusing suggestions for `error[E0599]: cannot write into String`

Open
#139,051 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-macros D-invalid-suggestion T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
fn main() {
    let mut s = String::new();
    writeln!(s,"{}", true).unwrap();
}
Current output
error[E0599]: cannot write into `String`
   --> .\test.rs:3:14
    |
3   |     writeln!(s,"{}", true).unwrap();
    |              ^
    |
   ::: C:\Users\jjl98\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\core\src\fmt\mod.rs:218:8
    |
218 |     fn write_fmt(&mut self, args: Arguments<'_>) -> Result {
    |        --------- the method is available for `String` here
    |
note: must implement `io::Write`, `fmt::Write`, or have a `write_fmt` method
   --> .\test.rs:3:14
    |
3   |     writeln!(s,"{}", true).unwrap();
    |              ^
    = 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;
    |
help: there is a method `write_str` with a similar name
   --> C:\Users\jjl98\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\core\src\macros\mod.rs:688:14
    |
688 |         $dst.write_str($crate::format_args_nl!($($arg)*))
    |              ~~~~~~~~~

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0599`.
Desired output
error[E0599]: cannot write into `String`
   --> .\test.rs:3:14
    |
3   |     writeln!(s,"{}", true).unwrap();
    |              ^
    |
   ::: C:\Users\jjl98\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\core\src\fmt\mod.rs:218:8
    |
218 |     fn write_fmt(&mut self, args: Arguments<'_>) -> Result {
    |        --------- the method is available for `String` here
    |
note: must implement `io::Write`, `fmt::Write`, or have a `write_fmt` method
   --> .\test.rs:3:14
    |
3   |     writeln!(s,"{}", true).unwrap();
    |              ^
    = 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;
    |

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0599`.
Rationale and extra context

The second suggestion in the error message is confusing. The content at line 688 of the file rustlib/src/rust\library\core\src\macros\mod.rs is $dst.write_fmt($crate::format_args_nl!($($arg)*)), which does not correspond to the definition of the write_str method.

Other cases

Rust Version
rustc 1.85.1 (4eb161250 2025-03-15)
binary: rustc
commit-hash: 4eb161250e340c8f48f66e2b929ef4a5bed7c181
commit-date: 2025-03-15
host: x86_64-pc-windows-msvc
release: 1.85.1
LLVM version: 19.1.7
Anything else?

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the diagnostic with the Rust example, then inspect rustlib/src/rust/library/core/src/macros/mod.rs around line 688 and the related compiler diagnostic code. Confirm why the write_str suggestion points to write_fmt, and make the completed diagnostic omit that misleading suggestion while preserving the useful trait-import help.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.