···11+{
22+ "blurb": "Given an age in seconds, calculate how old someone is in terms of a given planet's solar years.",
33+ "authors": [
44+ "IanWhitney"
55+ ],
66+ "contributors": [
77+ "ashleygwilliams",
88+ "bobahop",
99+ "coriolinus",
1010+ "cwhakes",
1111+ "durka",
1212+ "eddyp",
1313+ "efx",
1414+ "ErikSchierboom",
1515+ "IanWhitney",
1616+ "joshgoebel",
1717+ "lutostag",
1818+ "nfiles",
1919+ "ocstl",
2020+ "petertseng",
2121+ "rofrol",
2222+ "stringparser",
2323+ "xakon",
2424+ "ZapAnton"
2525+ ],
2626+ "files": {
2727+ "solution": [
2828+ "src/lib.rs"
2929+ ],
3030+ "test": [
3131+ "tests/space-age.rs"
3232+ ],
3333+ "example": [
3434+ ".meta/example.rs"
3535+ ]
3636+ },
3737+ "source": "Partially inspired by Chapter 1 in Chris Pine's online Learn to Program tutorial.",
3838+ "source_url": "http://pine.fm/LearnToProgram/?Chapter=01"
3939+}
+8
rust/space-age/.gitignore
···11+# Generated by Cargo
22+# will have compiled files and executables
33+/target/
44+**/*.rs.bk
55+66+# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
77+# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
88+Cargo.lock
···11+# Help
22+33+## Running the tests
44+55+Execute the tests with:
66+77+```bash
88+$ cargo test
99+```
1010+1111+All but the first test have been ignored. After you get the first test to
1212+pass, open the tests source file which is located in the `tests` directory
1313+and remove the `#[ignore]` flag from the next test and get the tests to pass
1414+again. Each separate test is a function with `#[test]` flag above it.
1515+Continue, until you pass every test.
1616+1717+If you wish to run _only ignored_ tests without editing the tests source file, use:
1818+1919+```bash
2020+$ cargo test -- --ignored
2121+```
2222+2323+If you are using Rust 1.51 or later, you can run _all_ tests with
2424+2525+```bash
2626+$ cargo test -- --include-ignored
2727+```
2828+2929+To run a specific test, for example `some_test`, you can use:
3030+3131+```bash
3232+$ cargo test some_test
3333+```
3434+3535+If the specific test is ignored, use:
3636+3737+```bash
3838+$ cargo test some_test -- --ignored
3939+```
4040+4141+To learn more about Rust tests refer to the online [test documentation][rust-tests].
4242+4343+[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
4444+4545+## Submitting your solution
4646+4747+You can submit your solution using the `exercism submit src/lib.rs` command.
4848+This command will upload your solution to the Exercism website and print the solution page's URL.
4949+5050+It's possible to submit an incomplete solution which allows you to:
5151+5252+- See how others have completed the exercise
5353+- Request help from a mentor
5454+5555+## Need to get help?
5656+5757+If you'd like help solving the exercise, check the following pages:
5858+5959+- The [Rust track's documentation](https://exercism.org/docs/tracks/rust)
6060+- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
6161+- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
6262+6363+Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
6464+6565+## Rust Installation
6666+6767+Refer to the [exercism help page][help-page] for Rust installation and learning
6868+resources.
6969+7070+## Submitting the solution
7171+7272+Generally you should submit all files in which you implemented your solution (`src/lib.rs` in most cases). If you are using any external crates, please consider submitting the `Cargo.toml` file. This will make the review process faster and clearer.
7373+7474+## Feedback, Issues, Pull Requests
7575+7676+The GitHub [track repository][github] is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help!
7777+7878+If you want to know more about Exercism, take a look at the [contribution guide].
7979+8080+## Submitting Incomplete Solutions
8181+It's possible to submit an incomplete solution so you can see how others have completed the exercise.
8282+8383+[help-page]: https://exercism.io/tracks/rust/learning
8484+[github]: https://github.com/exercism/rust
8585+[contribution guide]: https://exercism.io/docs/community/contributors
+69
rust/space-age/README.md
···11+# Space Age
22+33+Welcome to Space Age on Exercism's Rust Track.
44+If you need help running the tests or submitting your code, check out `HELP.md`.
55+66+## Instructions
77+88+Given an age in seconds, calculate how old someone would be on:
99+1010+ - Mercury: orbital period 0.2408467 Earth years
1111+ - Venus: orbital period 0.61519726 Earth years
1212+ - Earth: orbital period 1.0 Earth years, 365.25 Earth days, or 31557600 seconds
1313+ - Mars: orbital period 1.8808158 Earth years
1414+ - Jupiter: orbital period 11.862615 Earth years
1515+ - Saturn: orbital period 29.447498 Earth years
1616+ - Uranus: orbital period 84.016846 Earth years
1717+ - Neptune: orbital period 164.79132 Earth years
1818+1919+So if you were told someone were 1,000,000,000 seconds old, you should
2020+be able to say that they're 31.69 Earth-years old.
2121+2222+If you're wondering why Pluto didn't make the cut, go watch [this
2323+youtube video](http://www.youtube.com/watch?v=Z_2gbGXzFbs).
2424+2525+Some Rust topics you may want to read about while solving this problem:
2626+2727+- Traits, both the From trait and implementing your own traits
2828+- Default method implementations for traits
2929+- Macros, the use of a macro could reduce boilerplate and increase readability
3030+ for this exercise. For instance,
3131+ [a macro can implement a trait for multiple types at once](https://stackoverflow.com/questions/39150216/implementing-a-trait-for-multiple-types-at-once),
3232+ though it is fine to implement `years_during` in the Planet trait itself. A macro could
3333+ define both the structs and their implementations. Info to get started with macros can
3434+ be found at:
3535+3636+ - [The Macros chapter in The Rust Programming Language](https://doc.rust-lang.org/stable/book/ch19-06-macros.html)
3737+ - [an older version of the Macros chapter with helpful detail](https://doc.rust-lang.org/1.30.0/book/first-edition/macros.html)
3838+ - [Rust By Example](https://doc.rust-lang.org/stable/rust-by-example/macros.html)
3939+4040+## Source
4141+4242+### Created by
4343+4444+- @IanWhitney
4545+4646+### Contributed to by
4747+4848+- @ashleygwilliams
4949+- @bobahop
5050+- @coriolinus
5151+- @cwhakes
5252+- @durka
5353+- @eddyp
5454+- @efx
5555+- @ErikSchierboom
5656+- @IanWhitney
5757+- @joshgoebel
5858+- @lutostag
5959+- @nfiles
6060+- @ocstl
6161+- @petertseng
6262+- @rofrol
6363+- @stringparser
6464+- @xakon
6565+- @ZapAnton
6666+6767+### Based on
6868+6969+Partially inspired by Chapter 1 in Chris Pine's online Learn to Program tutorial. - http://pine.fm/LearnToProgram/?Chapter=01
+63
rust/space-age/src/lib.rs
···11+// The code below is a stub. Just enough to satisfy the compiler.
22+// In order to pass the tests you can add-to or change any of this code.
33+44+#[derive(Debug)]
55+pub struct Duration(f64);
66+77+impl From<u64> for Duration {
88+ fn from(s: u64) -> Self {
99+ Duration(((s as f64 / 31557600_f64) * 100.0).round() / 100.0)
1010+ }
1111+}
1212+1313+pub trait Planet {
1414+ fn years_during(d: &Duration) -> f64 {
1515+ d.0
1616+ }
1717+}
1818+1919+pub struct Mercury;
2020+pub struct Venus;
2121+pub struct Earth;
2222+pub struct Mars;
2323+pub struct Jupiter;
2424+pub struct Saturn;
2525+pub struct Uranus;
2626+pub struct Neptune;
2727+2828+impl Planet for Mercury {
2929+ fn years_during(d: &Duration) -> f64 {
3030+ d.0 / 0.2408467
3131+ }
3232+}
3333+impl Planet for Venus {
3434+ fn years_during(d: &Duration) -> f64 {
3535+ d.0 / 0.61519726
3636+ }
3737+}
3838+impl Planet for Earth {}
3939+impl Planet for Mars {
4040+ fn years_during(d: &Duration) -> f64 {
4141+ d.0 / 1.8808158
4242+ }
4343+}
4444+impl Planet for Jupiter {
4545+ fn years_during(d: &Duration) -> f64 {
4646+ d.0 / 11.862615
4747+ }
4848+}
4949+impl Planet for Saturn {
5050+ fn years_during(d: &Duration) -> f64 {
5151+ d.0 / 29.447498
5252+ }
5353+}
5454+impl Planet for Uranus {
5555+ fn years_during(d: &Duration) -> f64 {
5656+ d.0 / 84.016846
5757+ }
5858+}
5959+impl Planet for Neptune {
6060+ fn years_during(d: &Duration) -> f64 {
6161+ d.0 / 164.79132
6262+ }
6363+}
+60
rust/space-age/tests/space-age.rs
···11+use space_age::*;
22+33+fn assert_in_delta(expected: f64, actual: f64) {
44+ let diff: f64 = (expected - actual).abs();
55+ let delta: f64 = 0.01;
66+ if diff > delta {
77+ panic!(
88+ "Your result of {} should be within {} of the expected result {}",
99+ actual, delta, expected
1010+ )
1111+ }
1212+}
1313+1414+#[test]
1515+fn earth_age() {
1616+ let duration = Duration::from(1_000_000_000);
1717+ assert_in_delta(31.69, Earth::years_during(&duration));
1818+}
1919+2020+#[test]
2121+fn mercury_age() {
2222+ let duration = Duration::from(2_134_835_688);
2323+ assert_in_delta(280.88, Mercury::years_during(&duration));
2424+}
2525+2626+#[test]
2727+fn venus_age() {
2828+ let duration = Duration::from(189_839_836);
2929+ assert_in_delta(9.78, Venus::years_during(&duration));
3030+}
3131+3232+#[test]
3333+fn mars_age() {
3434+ let duration = Duration::from(2_129_871_239);
3535+ assert_in_delta(35.88, Mars::years_during(&duration));
3636+}
3737+3838+#[test]
3939+fn jupiter_age() {
4040+ let duration = Duration::from(901_876_382);
4141+ assert_in_delta(2.41, Jupiter::years_during(&duration));
4242+}
4343+4444+#[test]
4545+fn saturn_age() {
4646+ let duration = Duration::from(2_000_000_000);
4747+ assert_in_delta(2.15, Saturn::years_during(&duration));
4848+}
4949+5050+#[test]
5151+fn uranus_age() {
5252+ let duration = Duration::from(1_210_123_456);
5353+ assert_in_delta(0.46, Uranus::years_during(&duration));
5454+}
5555+5656+#[test]
5757+fn neptune_age() {
5858+ let duration = Duration::from(1_821_023_456);
5959+ assert_in_delta(0.35, Neptune::years_during(&duration));
6060+}