Skip to content

Commit a79bca6

Browse files
authored
exercise: add clock (#8)
1 parent 91a4921 commit a79bca6

File tree

9 files changed

+524
-1
lines changed

9 files changed

+524
-1
lines changed
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"authors": [
3+
"sacherjj"
4+
],
5+
"contributors": [
6+
"attilahorvath",
7+
"coriolinus",
8+
"cwhakes",
9+
"danieljl",
10+
"eddyp",
11+
"efx",
12+
"ErikSchierboom",
13+
"felix91gr",
14+
"kunaltyagi",
15+
"lutostag",
16+
"nfiles",
17+
"petertseng",
18+
"rofrol",
19+
"shaaraddalvi",
20+
"stringparser",
21+
"tmccombs",
22+
"xakon",
23+
"ZapAnton"
24+
],
25+
"files": {
26+
"solution": [
27+
"src/lib.rs",
28+
"Cargo.toml"
29+
],
30+
"test": [
31+
"tests/clock.rs"
32+
],
33+
"example": [
34+
".meta/example.rs"
35+
]
36+
},
37+
"blurb": "Implement a clock that handles times without dates.",
38+
"source": "Pairing session with Erin Drummond",
39+
"source_url": "https://twitter.com/ebdrummond",
40+
"custom": {
41+
"allowed-to-not-compile": "Stub doesn't compile because there is no to_string() implementation. This exercise is an introduction to derived and self-implemented traits, therefore adding template for a trait would reduce student learning."
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"track":"rust","exercise":"clock","id":"881fe91ec3d349b7bdadfa3e591006b2","url":"https://exercism.org/tracks/rust/exercises/clock","handle":"ruancomelli","is_requester":true,"auto_approve":false}

exercism/rust/clock/.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Generated by Cargo
2+
# will have compiled files and executables
3+
/target/
4+
**/*.rs.bk
5+
6+
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
7+
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
8+
Cargo.lock

exercism/rust/clock/Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
edition = "2021"
3+
name = "clock"
4+
version = "2.4.0"
5+
6+
[dependencies]

exercism/rust/clock/HELP.md

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Help
2+
3+
## Running the tests
4+
5+
Execute the tests with:
6+
7+
```bash
8+
$ cargo test
9+
```
10+
11+
All but the first test have been ignored. After you get the first test to
12+
pass, open the tests source file which is located in the `tests` directory
13+
and remove the `#[ignore]` flag from the next test and get the tests to pass
14+
again. Each separate test is a function with `#[test]` flag above it.
15+
Continue, until you pass every test.
16+
17+
If you wish to run _only ignored_ tests without editing the tests source file, use:
18+
19+
```bash
20+
$ cargo test -- --ignored
21+
```
22+
23+
If you are using Rust 1.51 or later, you can run _all_ tests with
24+
25+
```bash
26+
$ cargo test -- --include-ignored
27+
```
28+
29+
To run a specific test, for example `some_test`, you can use:
30+
31+
```bash
32+
$ cargo test some_test
33+
```
34+
35+
If the specific test is ignored, use:
36+
37+
```bash
38+
$ cargo test some_test -- --ignored
39+
```
40+
41+
To learn more about Rust tests refer to the online [test documentation][rust-tests].
42+
43+
[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
44+
45+
## Submitting your solution
46+
47+
You can submit your solution using the `exercism submit src/lib.rs Cargo.toml` command.
48+
This command will upload your solution to the Exercism website and print the solution page's URL.
49+
50+
It's possible to submit an incomplete solution which allows you to:
51+
52+
- See how others have completed the exercise
53+
- Request help from a mentor
54+
55+
## Need to get help?
56+
57+
If you'd like help solving the exercise, check the following pages:
58+
59+
- The [Rust track's documentation](https://exercism.org/docs/tracks/rust)
60+
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
61+
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
62+
63+
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
64+
65+
## Rust Installation
66+
67+
Refer to the [exercism help page][help-page] for Rust installation and learning
68+
resources.
69+
70+
## Submitting the solution
71+
72+
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.
73+
74+
## Feedback, Issues, Pull Requests
75+
76+
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!
77+
78+
If you want to know more about Exercism, take a look at the [contribution guide].
79+
80+
## Submitting Incomplete Solutions
81+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
82+
83+
[help-page]: https://exercism.org/tracks/rust/learning
84+
[github]: https://github.com/exercism/rust
85+
[contribution guide]: https://exercism.org/docs/community/contributors

exercism/rust/clock/README.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Clock
2+
3+
Welcome to Clock on Exercism's Rust Track.
4+
If you need help running the tests or submitting your code, check out `HELP.md`.
5+
6+
## Instructions
7+
8+
Implement a clock that handles times without dates.
9+
10+
You should be able to add and subtract minutes to it.
11+
12+
Two clocks that represent the same time should be equal to each other.
13+
14+
You will also need to implement `.to_string()` for the `Clock` struct. We will be using this to display the Clock's state. You can either do it via implementing it directly or using the [Display trait](https://doc.rust-lang.org/std/fmt/trait.Display.html).
15+
16+
Did you implement `.to_string()` for the `Clock` struct?
17+
18+
If so, try implementing the
19+
[Display trait](https://doc.rust-lang.org/std/fmt/trait.Display.html) for `Clock` instead.
20+
21+
Traits allow for a common way to implement functionality for various types.
22+
23+
For additional learning, consider how you might implement `String::from` for the `Clock` type.
24+
You don't have to actually implement this—it's redundant with `Display`, which is generally the
25+
better choice when the destination type is `String`—but it's useful to have a few type-conversion
26+
traits in your toolkit.
27+
28+
## Source
29+
30+
### Created by
31+
32+
- @sacherjj
33+
34+
### Contributed to by
35+
36+
- @attilahorvath
37+
- @coriolinus
38+
- @cwhakes
39+
- @danieljl
40+
- @eddyp
41+
- @efx
42+
- @ErikSchierboom
43+
- @felix91gr
44+
- @kunaltyagi
45+
- @lutostag
46+
- @nfiles
47+
- @petertseng
48+
- @rofrol
49+
- @shaaraddalvi
50+
- @stringparser
51+
- @tmccombs
52+
- @xakon
53+
- @ZapAnton
54+
55+
### Based on
56+
57+
Pairing session with Erin Drummond - https://twitter.com/ebdrummond

exercism/rust/clock/src/lib.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use std::fmt;
2+
3+
const MINUTES_PER_HOUR: i32 = 60;
4+
const MINUTES_PER_DAY: i32 = 24 * MINUTES_PER_HOUR;
5+
6+
7+
#[derive(fmt::Debug, PartialEq)]
8+
pub struct Clock(i32);
9+
10+
impl Clock {
11+
pub fn new(hours: i32, minutes: i32) -> Self {
12+
Clock((hours * MINUTES_PER_HOUR + minutes).rem_euclid(MINUTES_PER_DAY))
13+
}
14+
15+
pub fn add_minutes(&self, minutes: i32) -> Self {
16+
Clock::new(0, self.0 + minutes)
17+
}
18+
}
19+
20+
impl fmt::Display for Clock {
21+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
22+
write!(f, "{:02}:{:02}", self.0 / MINUTES_PER_HOUR, self.0 % MINUTES_PER_HOUR)
23+
}
24+
}

0 commit comments

Comments
 (0)