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
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
- Error – E0599
- No Method name
write-fmt
found for mutable referencemut string
in the current scope - Code
let _ = writeln! ( &mut s , "{}" , 5 );
- No Method name
- 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;
- Help
Code Revision
Outline
- Import Traits
- std::fmt::Write
- Allocate Objects
- Variable:- s
- let mut s = String::new();
- The string should be mutable
- let mut s = String::new();
- Variable:- s
- Write formatted string into the string buffer
- writeln! ( &mut s , “{}” , 5 );
- Print Buffer
- println!( “{}”, s );
- Deallocate Objects
- Variable:- s
- drop (s);
- Variable:- 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
- Drop Object
- Let _
- Writeln!
- Requirement
- Traits
- std::fmt::Write;
- Traits
- Requirement
- Platform Independence
- EOL
- Unix ( /n) Versus MS Windows ( /r/n )
- EOL
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
- std::fmt::Write;
There are a couple of traits that implement the writeln! interfaces.
The traits are:-
- std::io::Write
- 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
- Rust.variable.drop.using.let._.rs
Link