Skip to content

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions examples/staging/custom-fmt/input.md
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.
Copy link
Member

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.


This example demonstrates how to implement the `Show` trait used for
the default formatting argument: `{}`. Implemention of the other traits
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, do we need this   character here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On my screen it wrapped right before the {}, but it's not really an issue, I can remove it if you want...

is exactly the same - the `fmt` function is all that is needed.

See the docs for
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"See the docs for the full list of formatting traits"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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}
33 changes: 33 additions & 0 deletions examples/staging/custom-fmt/print-custom.rs
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use Show instead of fmt::Show

// 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'};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think that

let (lat, lat_c) = if self.lat >= 0.0 { (self.lat, 'N') } else { (-self.lat, 'S') }

may look clearer here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, how does the io::Writer relate to the rest of the arguments of write!?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll prefer self.lat.abs() rather than abs(self.lat) here. Same with self.lon

}
}

fn main() {
for city in [
City{name: "Vancouver", lat: 49.25, lon: -123.1,},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually format struct initializers as:

City { name: "Vancouver", lat: 49.25, lon: -123.1 },

The optional comma before a ], ) is only useful (for diff purposes) when the comma is the last character of the line.

City{name: "Dublin", lat: 53.347778, lon: -6.259722,},
City{name: "Oslo", lat: 59.95, lon: 10.75,},
].iter() {
println!("{}", city);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should also show that println!("{:d}", city} won't work because the Signed (?) trait is not implemented

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!

}
}
3 changes: 2 additions & 1 deletion examples/structure.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Copy link
Member

Choose a reason for hiding this comment

The 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 },
Expand Down