MCPcopy Create free account
hub / github.com/CSpyridakis/notes / fmt

Method fmt

Rust/notes.rs:117–130  ·  view source on GitHub ↗

This trait requires `fmt` with this exact signature.

(&self, f: &mut std::fmt::Formatter)

Source from the content-addressed store, hash-verified

115 impl std::fmt::Display for Point2D {
116 // This trait requires `fmt` with this exact signature.
117 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
118 // Write strictly the first element into the supplied output
119 // stream: `f`. Returns `fmt::Result` which indicates whether the
120 // operation succeeded or failed. Note that `write!` uses syntax which
121 // is very similar to `println!`.
122 // write!(f, "x: {}, y: {}", self.x, self.y) // If it was a tupple, we could access if like self.0, self.1
123
124 // Rust provides the ? operator, which can be combined with write! for complex writes
125 // Try `write!` to see if it errors. If it errors, return the error. Otherwise continue.
126 // As an example (This is the equivalent solution from the above):
127 // Use this trick for more complex structures that need to break in steps the print sequence
128 write!(f, "x: {}", self.x)?;
129 write!(f, ", y: {}", self.y)
130 }
131 }
132
133 let point = Point2D { x: 3.3, y: 7.2 };

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected