-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add a chapter on custom formatting to the stagin area #129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
The `std::fmt` module defines a number of traits each for one of the formatting | ||
argument types. | ||
|
||
This example demonstrates how to implement the `Show` trait used for | ||
the default formatting argument: `{}`. Implemention of the other traits | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, do we need this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On my screen it wrapped right before the |
||
is exactly the same - the `fmt` function is all that is needed. | ||
|
||
See the docs for | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "See the docs for the full list of formatting traits"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. I'm never sure about these articles... |
||
[full list](http://doc.rust-lang.org/std/fmt/index.html#formatting-traits) | ||
of formatting traits. | ||
|
||
{print-custom.play} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use std::fmt; | ||
use std::num::abs; | ||
|
||
struct City { | ||
name: &'static str, | ||
// Latitude | ||
lat: f32, | ||
// Longitude | ||
lon: f32, | ||
} | ||
|
||
impl fmt::Show for City { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use |
||
// Will output City's name as well as its location in a nice format | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
let lat_c = if self.lat >= 0.0 {'N'} else {'S'}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think that
may look clearer here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm... the code would be shorter, but not really clearer IMHO. Also note that that line spans over 80 chars... |
||
let lon_c = if self.lon >= 0.0 {'E'} else {'W'}; | ||
|
||
// `write!` writes formatted string into a `io::Writer` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, how does the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, not much admittedly... |
||
// and handles `Result` propagation | ||
write!(f, "{} ({:.3f}° {}, {:.3f}° {})", | ||
self.name, abs(self.lat), lat_c, abs(self.lon), lon_c) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll prefer |
||
} | ||
} | ||
|
||
fn main() { | ||
for city in [ | ||
City{name: "Vancouver", lat: 49.25, lon: -123.1,}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I usually format struct initializers as:
The optional comma before a |
||
City{name: "Dublin", lat: 53.347778, lon: -6.259722,}, | ||
City{name: "Oslo", lat: 59.95, lon: 10.75,}, | ||
].iter() { | ||
println!("{}", city); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should also show that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure! |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -107,7 +107,8 @@ | |
{ "id": "rand", "title": "Random", "children": null }, | ||
{ "id": "simd", "title": "SIMD", "children": null }, | ||
{ "id": "test", "title": "Testing", "children": null }, | ||
{ "id": "unsafe", "title": "Unsafe operations", "children": null } | ||
{ "id": "unsafe", "title": "Unsafe operations", "children": null }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file needs a rebase |
||
{ "id": "custom-fmt", "title": "Custom formatters", "children": null } | ||
] }, | ||
{ "id": "todo", "title": "TODO", "children": [ | ||
{ "id": "arg", "title": "Program arguments", "children": null }, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to introduce what "formatting argument types" are. I have opened #130 for this.