Exercism track submissions
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

space age with macro

+28 -45
+12 -45
rust/space-age/src/lib.rs
··· 1 1 // The code below is a stub. Just enough to satisfy the compiler. 2 2 // In order to pass the tests you can add-to or change any of this code. 3 3 4 + mod space_age_macro; 5 + 4 6 #[derive(Debug)] 5 7 pub struct Duration(f64); 6 8 ··· 16 18 } 17 19 } 18 20 19 - pub struct Mercury; 20 - pub struct Venus; 21 - pub struct Earth; 22 - pub struct Mars; 23 - pub struct Jupiter; 24 - pub struct Saturn; 25 - pub struct Uranus; 26 - pub struct Neptune; 27 - 28 - impl Planet for Mercury { 29 - fn years_during(d: &Duration) -> f64 { 30 - d.0 / 0.2408467 31 - } 32 - } 33 - impl Planet for Venus { 34 - fn years_during(d: &Duration) -> f64 { 35 - d.0 / 0.61519726 36 - } 37 - } 38 - impl Planet for Earth {} 39 - impl Planet for Mars { 40 - fn years_during(d: &Duration) -> f64 { 41 - d.0 / 1.8808158 42 - } 43 - } 44 - impl Planet for Jupiter { 45 - fn years_during(d: &Duration) -> f64 { 46 - d.0 / 11.862615 47 - } 48 - } 49 - impl Planet for Saturn { 50 - fn years_during(d: &Duration) -> f64 { 51 - d.0 / 29.447498 52 - } 53 - } 54 - impl Planet for Uranus { 55 - fn years_during(d: &Duration) -> f64 { 56 - d.0 / 84.016846 57 - } 58 - } 59 - impl Planet for Neptune { 60 - fn years_during(d: &Duration) -> f64 { 61 - d.0 / 164.79132 62 - } 63 - } 21 + space_age! ( 22 + Mercury -> 0.2408467, 23 + Venus -> 0.61519726, 24 + Earth -> 1.0, 25 + Mars -> 1.8808158, 26 + Jupiter -> 11.862615, 27 + Saturn -> 29.447498, 28 + Uranus -> 84.016846, 29 + Neptune -> 164.79132 30 + );
+16
rust/space-age/src/space_age_macro.rs
··· 1 + #[macro_export] 2 + macro_rules! space_age { 3 + ( 4 + $($planet:ident -> $distance:tt),+ 5 + ) => { 6 + 7 + $( 8 + pub struct $planet; 9 + impl Planet for $planet { 10 + fn years_during(d: &Duration) -> f64 { 11 + d.0 / $distance 12 + } 13 + } 14 + )+ 15 + } 16 + }