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

Conversation

vojtechkral
Copy link
Contributor

Using cities like in json :-)
I have no idea where this could go in the structure since traits are a bit too advanced a topic for the 2nd chapter...

@japaric
Copy link
Member

japaric commented Jun 19, 2014

TIL: How to implement the Show trait

@@ -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.

@japaric
Copy link
Member

japaric commented Jun 19, 2014

Thanks for the PR!

I like the example. I was wondering if we should also implement another formatting trait (like {:d}) for completeness purposes.

Order-wise, this should go after the "traits" chapter

@vojtechkral
Copy link
Contributor Author

Ok, I'm gonna fix the issues...

I was wondering if we should also implement another formatting trait (like {:d}) for completeness purposes.

Yeah, I was thinking about that too, but the Cities example is not really suitable for that IMO. How about this:

enum StoneAgeNumber {
    One,
    Two,
    Many,
}

and have it print "one", "two", "many" for {:s} and "1", "2", ">2" for {:d}

@japaric
Copy link
Member

japaric commented Jun 30, 2014

@vojtechkral Sounds good to me!

@vojtechkral
Copy link
Contributor Author

@japaric I'm in the process of updating this PR, there's a bit of a strange thing going on with the fmt::String trait, I posted about on reddit: http://redd.it/29tums

@vojtechkral
Copy link
Contributor Author

@japaric I'm still struggling to lay out this chapter so that it makes sense. Currently only one playpen editor per page is allowed, so I'd have to move the StoneAgeNumber example into subchapter. Or perhaps I could replace the earlier code ... Do you think that the StoneAgeNumber example makes the Cities example redundant?

@japaric
Copy link
Member

japaric commented Jul 15, 2014

I'll re-review this later today, but I want to drop some notes now (or I might forget):

  • I think the most used formatting trait is the Show trait, so we should focus on that one.
  • The other traits are mostly implemented by numeric types, which allow different representations. These traits should be covered in a second example. I think an Inch(T) type may work, it'll get formatted as 1000 inches/1e3 inches or something like that.
  • I've been implemented the Show trait lately, and I have noticed that if I use impl Show for Foo ... write!(f, "Foo") then using println!("{:>8}", Foo) will ignore the padding parameter! So instead of using the write! macro, one should use the f.pad("foo") method instead - as this method uses the padding information. I think this piece of information should be covered.

@japaric japaric closed this in d42986e Jul 26, 2014
@vojtechkral vojtechkral deleted the custom-fmt branch July 26, 2014 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants