Skip to content

Instantly share code, notes, and snippets.

@khle
Created August 30, 2025 01:42
Show Gist options
  • Save khle/229f7e36506e731f625cb737dbccefbe to your computer and use it in GitHub Desktop.
Save khle/229f7e36506e731f625cb737dbccefbe to your computer and use it in GitHub Desktop.
// 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.
//My first naive solution to https://exercism.org/tracks/rust/exercises/space-age
#[derive(Debug)]
pub struct Duration {
seconds: u64,
}
impl From<u64> for Duration {
fn from(s: u64) -> Self {
Duration {seconds: s}
}
}
pub trait Planet {
fn years_during(d: &Duration) -> f64 {
d.seconds as f64 / Self::seconds_in_one_earth_year()
}
fn seconds_in_one_earth_year() -> f64;
}
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 seconds_in_one_earth_year() -> f64 {
31_557_600 as f64 * 0.2408467
}
}
impl Planet for Venus {
fn seconds_in_one_earth_year() -> f64 {
31_557_600 as f64 * 0.61519726
}
}
impl Planet for Earth {
fn seconds_in_one_earth_year() -> f64 {
println!("Earth");
31_557_600 as f64 * 1.0
}
}
impl Planet for Mars {
fn seconds_in_one_earth_year() -> f64 {
println!("Mars");
31_557_600 as f64 * 1.8808158
}
}
impl Planet for Jupiter {
fn seconds_in_one_earth_year() -> f64 {
println!("Jupiter");
31_557_600 as f64 * 11.862615
}
}
impl Planet for Saturn {
fn seconds_in_one_earth_year() -> f64 {
println!("Saturn");
31_557_600 as f64 * 29.447498
}
}
impl Planet for Uranus {
fn seconds_in_one_earth_year() -> f64 {
println!("Uranus");
31_557_600 as f64 * 84.016846
}
}
impl Planet for Neptune {
fn seconds_in_one_earth_year() -> f64 {
println!("Neptune");
31_557_600 as f64 * 164.79132
}
}
// 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(s: u64) -> Self {
Duration {seconds: s}
}
}
pub trait Planet {
fn years_during(d: &Duration) -> f64 {
d.seconds as f64 / Self::seconds_in_one_earth_year()
}
fn seconds_in_one_earth_year() -> f64;
}
//pub trait Orbit {
// fn seconds_in_one_earth_year() -> f64;
//}
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 seconds_in_one_earth_year() -> f64 {
31_557_600 as f64 * 0.2408467
}
}
impl Planet for Venus {
fn seconds_in_one_earth_year() -> f64 {
31_557_600 as f64 * 0.61519726
}
}
impl Planet for Earth {
fn seconds_in_one_earth_year() -> f64 {
println!("Earth");
31_557_600 as f64 * 1.0
}
}
impl Planet for Mars {
fn seconds_in_one_earth_year() -> f64 {
println!("Mars");
31_557_600 as f64 * 1.8808158
}
}
impl Planet for Jupiter {
fn seconds_in_one_earth_year() -> f64 {
println!("Jupiter");
31_557_600 as f64 * 11.862615
}
}
impl Planet for Saturn {
fn seconds_in_one_earth_year() -> f64 {
println!("Saturn");
31_557_600 as f64 * 29.447498
}
}
impl Planet for Uranus {
fn seconds_in_one_earth_year() -> f64 {
println!("Uranus");
31_557_600 as f64 * 84.016846
}
}
impl Planet for Neptune {
fn seconds_in_one_earth_year() -> f64 {
println!("Neptune");
31_557_600 as f64 * 164.79132
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment