11: Add Rust track exercise "Space Age".

[?]
9Zb2bmkejrNknawUtr3MKvVstZkVDR8x8ritfgZXKrky
Dec 23, 2021, 5:33 PM
VXRUG632SEYNKNTQ4N6PNQHXIGUHKGLIBN457BIZYSCYSPNTW25QC

Dependencies

  • [2] ERE2X4MY 1: Add Rust track exercise "Hello World".

Change contents

  • file addition: space-age (d--r------)
    [2.16]
  • file addition: tests (d--r------)
    [0.21]
  • file addition: space-age.rs (----------)
    [0.40]
    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));
    }
  • file addition: src (d--r------)
    [0.21]
  • file addition: lib.rs (----------)
    [0.1589]
    // 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 {}
  • file addition: README.md (----------)
    [0.21]
    # Space Age
    Welcome to Space Age on Exercism's Rust Track.
    If you need help running the tests or submitting your code, check out `HELP.md`.
    ## Instructions
    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](http://www.youtube.com/watch?v=Z_2gbGXzFbs).
    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
    - Macros, the use of a macro could reduce boilerplate and increase readability
    for this exercise. For instance,
    [a macro can implement a trait for multiple types at once](https://stackoverflow.com/questions/39150216/implementing-a-trait-for-multiple-types-at-once),
    though it is fine to implement `years_during` in the Planet trait itself. A macro could
    define both the structs and their implementations. Info to get started with macros can
    be found at:
    - [The Macros chapter in The Rust Programming Language](https://doc.rust-lang.org/stable/book/ch19-06-macros.html)
    - [an older version of the Macros chapter with helpful detail](https://doc.rust-lang.org/1.30.0/book/first-edition/macros.html)
    - [Rust By Example](https://doc.rust-lang.org/stable/rust-by-example/macros.html)
    ## Source
    ### Created by
    - @IanWhitney
    ### Contributed to by
    - @ashleygwilliams
    - @bobahop
    - @coriolinus
    - @cwhakes
    - @durka
    - @eddyp
    - @efx
    - @ErikSchierboom
    - @IanWhitney
    - @joshgoebel
    - @lutostag
    - @nfiles
    - @ocstl
    - @petertseng
    - @rofrol
    - @stringparser
    - @xakon
    - @ZapAnton
    ### Based on
    Partially inspired by Chapter 1 in Chris Pine's online Learn to Program tutorial. - http://pine.fm/LearnToProgram/?Chapter=01
  • file addition: HELP.md (----------)
    [0.21]
    # Help
    ## Running the tests
    Execute the tests with:
    ```bash
    $ 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 _only ignored_ tests without editing the tests source file, use:
    ```bash
    $ cargo test -- --ignored
    ```
    If you are using Rust 1.51 or later, you can run _all_ tests with
    ```bash
    $ cargo test -- --include-ignored
    ```
    To run a specific test, for example `some_test`, you can use:
    ```bash
    $ cargo test some_test
    ```
    If the specific test is ignored, use:
    ```bash
    $ cargo test some_test -- --ignored
    ```
    To learn more about Rust tests refer to the online [test documentation][rust-tests].
    [rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
    ## Submitting your solution
    You can submit your solution using the `exercism submit src/lib.rs` command.
    This command will upload your solution to the Exercism website and print the solution page's URL.
    It's possible to submit an incomplete solution which allows you to:
    - See how others have completed the exercise
    - Request help from a mentor
    ## Need to get help?
    If you'd like help solving the exercise, check the following pages:
    - The [Rust track's documentation](https://exercism.org/docs/tracks/rust)
    - [Exercism's support channel on gitter](https://gitter.im/exercism/support)
    - The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
    Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
    ## Rust Installation
    Refer to the [exercism help page][help-page] for Rust installation and learning
    resources.
    ## 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 GitHub [track repository][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].
    ## Submitting Incomplete Solutions
    It's possible to submit an incomplete solution so you can see how others have completed the exercise.
    [help-page]: https://exercism.io/tracks/rust/learning
    [github]: https://github.com/exercism/rust
    [contribution guide]: https://exercism.io/docs/community/contributors
  • file addition: Cargo.toml (----------)
    [0.21]
    [package]
    edition = "2021"
    name = "space-age"
    version = "1.2.0"
  • file addition: .gitignore (----------)
    [0.21]
    # Generated by Cargo
    # will have compiled files and executables
    /target/
    **/*.rs.bk
    # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
    # More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
    Cargo.lock
  • file addition: .exercism (d--r------)
    [0.21]
  • file addition: metadata.json (----------)
    [0.8140]
    {"track":"rust","exercise":"space-age","id":"8709480ff0a442c0a99befcc9cbeb594","url":"https://exercism.org/tracks/rust/exercises/space-age","handle":"nicoty","is_requester":true,"auto_approve":false}
  • file addition: config.json (----------)
    [0.8140]
    {
    "blurb": "Given an age in seconds, calculate how old someone is in terms of a given planet's solar years.",
    "authors": [
    "IanWhitney"
    ],
    "contributors": [
    "ashleygwilliams",
    "bobahop",
    "coriolinus",
    "cwhakes",
    "durka",
    "eddyp",
    "efx",
    "ErikSchierboom",
    "IanWhitney",
    "joshgoebel",
    "lutostag",
    "nfiles",
    "ocstl",
    "petertseng",
    "rofrol",
    "stringparser",
    "xakon",
    "ZapAnton"
    ],
    "files": {
    "solution": [
    "src/lib.rs"
    ],
    "test": [
    "tests/space-age.rs"
    ],
    "example": [
    ".meta/example.rs"
    ]
    },
    "source": "Partially inspired by Chapter 1 in Chris Pine's online Learn to Program tutorial.",
    "source_url": "http://pine.fm/LearnToProgram/?Chapter=01"
    }