Skip to content

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

@jjl9807

Description

@jjl9807

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions