ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# n0mn0m's solution

## to Space Age in the Rust Track

Published at Oct 27 2020 · 0 comments
Instructions
Test suite
Solution

Given an age in seconds, calculate how old someone would be on:

• Mercury: orbital period 0.2408467 Earth years
• Venus: orbital period 0.61519726 Earth years
• Earth: orbital period 1.0 Earth years, 365.25 Earth days, or 31557600 seconds
• Mars: orbital period 1.8808158 Earth years
• Jupiter: orbital period 11.862615 Earth years
• Saturn: orbital period 29.447498 Earth years
• Uranus: orbital period 84.016846 Earth years
• Neptune: orbital period 164.79132 Earth years

So if you were told someone were 1,000,000,000 seconds old, you should be able to say that they're 31.69 Earth-years old.

If you're wondering why Pluto didn't make the cut, go watch this youtube video.

# Topics

Some Rust topics you may want to read about while solving this problem:

• Traits, both the From trait and implementing your own traits
• Default method implementations for traits

## Rust Installation

Refer to the exercism help page for Rust installation and learning resources.

## Writing the Code

Execute the tests with:

``````\$ cargo test
``````

All but the first test have been ignored. After you get the first test to pass, open the tests source file which is located in the `tests` directory and remove the `#[ignore]` flag from the next test and get the tests to pass again. Each separate test is a function with `#[test]` flag above it. Continue, until you pass every test.

If you wish to run all ignored tests without editing the tests source file, use:

``````\$ cargo test -- --ignored
``````

To run a specific test, for example `some_test`, you can use:

``````\$ cargo test some_test
``````

If the specific test is ignored use:

``````\$ cargo test some_test -- --ignored
``````

## Further improvements

After you have solved the exercise, please consider using the additional utilities, described in the installation guide, to further refine your final solution.

To format your solution, inside the solution directory use

``````cargo fmt
``````

To see, if your solution contains some common ineffective use cases, inside the solution directory use

``````cargo clippy --all-targets
``````

## Submitting the solution

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.

## Feedback, Issues, Pull Requests

The exercism/rust repository on 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!

If you want to know more about Exercism, take a look at the contribution guide.

## Source

Partially inspired by Chapter 1 in Chris Pine's online Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=01

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### space-age.rs

``````use space_age::*;

fn assert_in_delta(expected: f64, actual: f64) {
let diff: f64 = (expected - actual).abs();
let delta: f64 = 0.01;
if diff > delta {
panic!(
"Your result of {} should be within {} of the expected result {}",
actual, delta, expected
)
}
}

#[test]
fn earth_age() {
let duration = Duration::from(1_000_000_000);
assert_in_delta(31.69, Earth::years_during(&duration));
}

#[test]
#[ignore]
fn mercury_age() {
let duration = Duration::from(2_134_835_688);
assert_in_delta(280.88, Mercury::years_during(&duration));
}

#[test]
#[ignore]
fn venus_age() {
let duration = Duration::from(189_839_836);
assert_in_delta(9.78, Venus::years_during(&duration));
}

#[test]
#[ignore]
fn mars_age() {
let duration = Duration::from(2_129_871_239);
assert_in_delta(35.88, Mars::years_during(&duration));
}

#[test]
#[ignore]
fn jupiter_age() {
let duration = Duration::from(901_876_382);
assert_in_delta(2.41, Jupiter::years_during(&duration));
}

#[test]
#[ignore]
fn saturn_age() {
let duration = Duration::from(2_000_000_000);
assert_in_delta(2.15, Saturn::years_during(&duration));
}

#[test]
#[ignore]
fn uranus_age() {
let duration = Duration::from(1_210_123_456);
assert_in_delta(0.46, Uranus::years_during(&duration));
}

#[test]
#[ignore]
fn neptune_age() {
let duration = Duration::from(1_821_023_456);
assert_in_delta(0.35, Neptune::years_during(&duration));
}``````
``````// The code below is a stub. Just enough to satisfy the compiler.
// In order to pass the tests you can add-to or change any of this code.

#[derive(Debug)]
pub struct Duration {
seconds: u64,
}

impl From<u64> for Duration {
fn from(seconds: u64) -> Self {
Duration { seconds }
}
}

pub trait Planet {
fn years_during(d: &Duration) -> f64;
}

macro_rules! planet_impl {
(\$type:ident, \$earth_years:expr) => {
pub struct \$type;

impl Planet for \$type {
fn years_during(d: &Duration) -> f64 {
d.seconds as f64 / (31_557_600 as f64 * \$earth_years)
}
}
};
}

planet_impl!(Mercury, 0.240_846_7);
planet_impl!(Venus, 0.615_197_26);
planet_impl!(Earth, 1.0);
planet_impl!(Mars, 1.880_815_8);
planet_impl!(Jupiter, 11.862_615);
planet_impl!(Saturn, 29.447_498);
planet_impl!(Uranus, 84.016_846);
planet_impl!(Neptune, 164.79132);``````