Rust:- Compilation – Error – “error[E0599]: no method named `write_fmt` found for mutable reference `&mut String` in the current scope”

Background

Like often said on the streets, always behind that boy.

Always, behind that girl.

As for me, always behind that stolen code.

 

Code Sample

rust-lang.org

Formatln! = format! + platform specific newline

XAMPPRocky

Link

Here is the code sample.

Text


let mut s = String::new();
let _ = writeln!(&mut s, "{}", 5);

Lab Work

If curiosity gets to you, and you take that code to the lab.

You might stumble a bit.

Lab Code

Code Original

Code Snippet


fn codeOriginal() 
{

    let mut s = String::new();
    
    let _ = writeln!
                (
                      &mut s
                    , "{}"
                    , 5
                );
    
}

Output

Output – Textual

error[E0599]: no method named `write_fmt` found for mutable reference `&mut String` in the current scope
  --> main.rs:19:13
   |
19 |       let _ = writeln!
   |  _____________^
20 | |                 (
21 | |                       &mut s
22 | |                     , "{}"
23 | |                     , 5
24 | |                 );
   | |_________________^ method not found in `&mut String`
   |
   = help: items from traits can only be used if the trait is in scope
   = note: this error originates in the macro `writeln` (in Nightly builds, run with -Z macro-backtrace for more info)
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
   |
14 | use std::fmt::Write;
   |
error: aborting due to previous error
For more information about this error, try `rustc --explain E0599`.

 

Output – Image

Explanation

  1. Error – E0599
    • No Method name write-fmt found for mutable reference mut string in the current scope
    • Code
      let _ = writeln!
              (
                    &mut s
                  , "{}"
                  , 5
              );
      
  2. Guidance
    • Help
      • help: items from traits can only be used if the trait is in scope
      • help: the following trait is implemented but not in scope; perhaps add a `use` for it
        • use std::fmt::Write;

 

Code Revision

Outline

  1. Import Traits
    • std::fmt::Write
  2. Allocate Objects
    • Variable:- s
      • let mut s = String::new();
        • The string should be mutable
  3. Write formatted string into the string buffer
    • writeln! ( &mut s , “{}” , 5 );
  4. Print Buffer
    • println!( “{}”, s );
  5. Deallocate Objects
    • Variable:- s
      • drop (s);

Code Snippet


fn codeRevised() 
{

    /*
        Use Trait std::fmt::Write ( Formatting Trait )
    */
    use std::fmt::Write;
    
    /*
        Allocate String Object (s)
    */
    let mut s = String::new();
    
    /*
        write formatted string into string object
    */
    writeln!
    (
           &mut s
         , "{}"
         , 5
    );
                
    /*
        println object s
    */
    println!
        (
              "{}"
            , s
        );
        
    //drop allocated object s
    drop(s);
    
     
}

 

Lessons Learned

Outline

  1. Drop Object
    • Let _
  2. Writeln!
    • Requirement
      • Traits
        • std::fmt::Write;
  3. Platform Independence
    • EOL
      • Unix ( /n) Versus MS Windows ( /r/n )

 

Drop Object

Let _

The original code reads:-


let _ = writeln! ( &mut s , "{}" , 5 );

It appears that at one time, let _ means drop object after use.

Modernly, we can issue drop(<variable>);

 

Writeln!

Requirement

Traits

  1. std::fmt::Write;

There are a couple of traits that implement the writeln! interfaces.

The traits are:-

  1. std::io::Write
  2. std::fmt::Write

In our case, we are employing writeln! to format 5 as a string.

 

Platform Independence

EOL

Unix ( /n ) Versus MS Windows ( /r/n )

Background

Traditionally, Linux uses Line Feeed ( /n ).

On the other hand, MS Windows uses Carriage Return ( CR )  [ \r ] and Line Feed ( LF ) [ \n ].

Seemingly, Rust bridges the gap and one only needs Line Feed ( \n ).

Online Source Code

Online GDB

  1. Rust.variable.drop.using.let._.rs
    Link

 

References

  1. rust-lang.org
    • docs
    • internals
      • Formatln! = format! + platform specific newline
        Link
      • Writeln!() macro missing important import
        Link

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s