Exercism space age

Hello,

This part is given

// 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;

impl From<u64> for Duration {
    fn from(s: u64) -> Self {
        unimplemented!("s, measured in seconds: {}", s)
    }
}

pub trait Planet {
    fn years_during(d: &Duration) -> f64 {
        unimplemented!(
            "convert a duration ({:?}) to the number of years on this planet for that duration",
            d,
        );
    }
}

pub struct Mercury;
pub struct Venus;
pub struct Earth;
pub struct Mars;
pub struct Jupiter;
pub struct Saturn;
pub struct Uranus;
pub struct Neptune;

impl Planet for Mercury {}
impl Planet for Venus {}
impl Planet for Earth {}
impl Planet for Mars {}
impl Planet for Jupiter {}
impl Planet for Saturn {}
impl Planet for Uranus {}
impl Planet for Neptune {}

do I have it right that duration is the duration of a planet so lets say the input is 3 years on earth. Is duration then the years on that planet ?

but what is then expected to be calculated on years_during in the trait ?

Roelof

A second is an amount of time that’s the same everywhere (ignoring relativity). A year, by contrast, is the amount of time it takes a particular planet to make a complete orbit around the sun.

The goal of years_during, then, is to calculate how many laps around the sun the given planet makes in the given Duration.

moment,

This is the explanation of the challenge

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

    Earth: orbital period 365.25 Earth days, or 31557600 seconds
    Mercury: orbital period 0.2408467 Earth years
    Venus: orbital period 0.61519726 Earth years
    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.

Hmm...

My understanding is that a second is an amount of time that is the same everywhere even including relativity.

But yeah, Einstein is not relevant to this question.

Sort of. A second is defined as 9192631770 oscillations of a Cs-133 atom, so in that sense it’s always the same. But how many of those oscillations actually happens between two instants is dependent on the observer’s location during the interval being measured (to the extent that “instant” has a useful meaning in Einstein’s theory).

1 Like

correct this is going off topic

This is 1,000,000,000 seconds / 365.25 days, and a similar calculation is required for the other planets (but with a different denominator).

My question is what must I do in the two methods ?
The calculation I figured out already

Construct a Duration that represents s seconds:

impl From<u64> for Duration {
    fn from(s: u64) -> Self {
        unimplemented!("s, measured in seconds: {}", s)
    }
}

Calculate the answer to the problem, with age in seconds coming from d and the orbital period coming from Self:

pub trait Planet {
    fn years_during(d: &Duration) -> f64 {
        unimplemented!(
            "convert a duration ({:?}) to the number of years on this planet for that duration",
            d,
        );
    }
}

It might help to look at the other solutions posted for that problem on exercism: