From 007af76cc65ffbbb4b3dd4afdfe150af24f04436 Mon Sep 17 00:00:00 2001 From: "exercism-solutions-syncer[bot]" <211797793+exercism-solutions-syncer[bot]@users.noreply.github.com> Date: Wed, 27 Aug 2025 13:18:43 +0000 Subject: [PATCH] [Sync Iteration] rust/space-age/1 --- solutions/rust/space-age/1/src/lib.rs | 89 +++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 solutions/rust/space-age/1/src/lib.rs diff --git a/solutions/rust/space-age/1/src/lib.rs b/solutions/rust/space-age/1/src/lib.rs new file mode 100644 index 0000000..84092e5 --- /dev/null +++ b/solutions/rust/space-age/1/src/lib.rs @@ -0,0 +1,89 @@ +// 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 { + earth_years: f64, +} + +impl From for Duration { + fn from(s: u64) -> Self { + const SECONDS_IN_HOUR: f64 = 3600.0; + const HOURS_IN_DAY: f64 = 24.0; + const DAYS_IN_YEAR: f64 = 365.25; + + Duration { + earth_years: s as f64 / (SECONDS_IN_HOUR * HOURS_IN_DAY * DAYS_IN_YEAR), + } + } +} + +pub trait Planet { + fn years_during(d: &Duration) -> f64 { + todo!("convert a duration ({d:?}) to the number of years on this planet for that duration"); + } +} + +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 { + fn years_during(d: &Duration) -> f64 { + const EARTH_YEARS_ON_MERCURY: f64 = 0.2408467; + + d.earth_years / EARTH_YEARS_ON_MERCURY + } +} +impl Planet for Venus { + fn years_during(d: &Duration) -> f64 { + const EARTH_YEARS_ON_VENUS: f64 = 0.61519726; + + d.earth_years / EARTH_YEARS_ON_VENUS + } +} +impl Planet for Earth { + fn years_during(d: &Duration) -> f64 { + d.earth_years + } +} +impl Planet for Mars { + fn years_during(d: &Duration) -> f64 { + const EARTH_YEARS_ON_MARS: f64 = 1.8808158; + + d.earth_years / EARTH_YEARS_ON_MARS + } +} +impl Planet for Jupiter { + fn years_during(d: &Duration) -> f64 { + const EARTH_YEARS_ON_JUPITER: f64 = 11.862615; + + d.earth_years / EARTH_YEARS_ON_JUPITER + } +} +impl Planet for Saturn { + fn years_during(d: &Duration) -> f64 { + const EARTH_YEARS_ON_SATURN: f64 = 29.447498; + + d.earth_years / EARTH_YEARS_ON_SATURN + } +} +impl Planet for Uranus { + fn years_during(d: &Duration) -> f64 { + const EARTH_YEARS_ON_URANUS: f64 = 84.016846; + + d.earth_years / EARTH_YEARS_ON_URANUS + } +} +impl Planet for Neptune { + fn years_during(d: &Duration) -> f64 { + const EARTH_YEARS_ON_NEPTUNE: f64 = 164.79132; + + d.earth_years / EARTH_YEARS_ON_NEPTUNE + } +}