diff options
| author | Federico Igne <git@federicoigne.com> | 2020-12-26 17:48:38 +0000 |
|---|---|---|
| committer | Federico Igne <git@federicoigne.com> | 2021-11-03 18:55:08 +0000 |
| commit | 02481656966b0a8dfc95cf3c22bcc049660ff7d4 (patch) | |
| tree | 8e39798fcaf27931d91c2088423fd4e97adcfc2d /rust/beer-song | |
| parent | 4e2052c4d792540c2f742b2c2a081b11117ed41d (diff) | |
| download | exercism-02481656966b0a8dfc95cf3c22bcc049660ff7d4.tar.gz exercism-02481656966b0a8dfc95cf3c22bcc049660ff7d4.zip | |
Move Rust exercises in a subdirectory
Diffstat (limited to 'rust/beer-song')
| -rw-r--r-- | rust/beer-song/.exercism/metadata.json | 1 | ||||
| -rw-r--r-- | rust/beer-song/.gitignore | 8 | ||||
| -rw-r--r-- | rust/beer-song/Cargo.toml | 7 | ||||
| -rw-r--r-- | rust/beer-song/README.md | 401 | ||||
| -rw-r--r-- | rust/beer-song/src/intersperse.rs | 52 | ||||
| -rw-r--r-- | rust/beer-song/src/lib.rs | 20 | ||||
| -rw-r--r-- | rust/beer-song/tests/beer-song.rs | 31 |
7 files changed, 520 insertions, 0 deletions
diff --git a/rust/beer-song/.exercism/metadata.json b/rust/beer-song/.exercism/metadata.json new file mode 100644 index 0000000..640fbbc --- /dev/null +++ b/rust/beer-song/.exercism/metadata.json | |||
| @@ -0,0 +1 @@ | |||
| {"track":"rust","exercise":"beer-song","id":"d7320f8ac9c543dc853815bbb5b4d4d5","url":"https://exercism.io/my/solutions/d7320f8ac9c543dc853815bbb5b4d4d5","handle":"dyamon","is_requester":true,"auto_approve":false} \ No newline at end of file | |||
diff --git a/rust/beer-song/.gitignore b/rust/beer-song/.gitignore new file mode 100644 index 0000000..db7f315 --- /dev/null +++ b/rust/beer-song/.gitignore | |||
| @@ -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 | ||
diff --git a/rust/beer-song/Cargo.toml b/rust/beer-song/Cargo.toml new file mode 100644 index 0000000..ff6683e --- /dev/null +++ b/rust/beer-song/Cargo.toml | |||
| @@ -0,0 +1,7 @@ | |||
| 1 | [package] | ||
| 2 | edition = "2018" | ||
| 3 | name = "beer-song" | ||
| 4 | version = "0.0.0" | ||
| 5 | |||
| 6 | [dependencies] | ||
| 7 | itertools = "0.9" | ||
diff --git a/rust/beer-song/README.md b/rust/beer-song/README.md new file mode 100644 index 0000000..2ed305c --- /dev/null +++ b/rust/beer-song/README.md | |||
| @@ -0,0 +1,401 @@ | |||
| 1 | # Beer Song | ||
| 2 | |||
| 3 | Recite the lyrics to that beloved classic, that field-trip favorite: 99 Bottles of Beer on the Wall. | ||
| 4 | |||
| 5 | Note that not all verses are identical. | ||
| 6 | |||
| 7 | ```text | ||
| 8 | 99 bottles of beer on the wall, 99 bottles of beer. | ||
| 9 | Take one down and pass it around, 98 bottles of beer on the wall. | ||
| 10 | |||
| 11 | 98 bottles of beer on the wall, 98 bottles of beer. | ||
| 12 | Take one down and pass it around, 97 bottles of beer on the wall. | ||
| 13 | |||
| 14 | 97 bottles of beer on the wall, 97 bottles of beer. | ||
| 15 | Take one down and pass it around, 96 bottles of beer on the wall. | ||
| 16 | |||
| 17 | 96 bottles of beer on the wall, 96 bottles of beer. | ||
| 18 | Take one down and pass it around, 95 bottles of beer on the wall. | ||
| 19 | |||
| 20 | 95 bottles of beer on the wall, 95 bottles of beer. | ||
| 21 | Take one down and pass it around, 94 bottles of beer on the wall. | ||
| 22 | |||
| 23 | 94 bottles of beer on the wall, 94 bottles of beer. | ||
| 24 | Take one down and pass it around, 93 bottles of beer on the wall. | ||
| 25 | |||
| 26 | 93 bottles of beer on the wall, 93 bottles of beer. | ||
| 27 | Take one down and pass it around, 92 bottles of beer on the wall. | ||
| 28 | |||
| 29 | 92 bottles of beer on the wall, 92 bottles of beer. | ||
| 30 | Take one down and pass it around, 91 bottles of beer on the wall. | ||
| 31 | |||
| 32 | 91 bottles of beer on the wall, 91 bottles of beer. | ||
| 33 | Take one down and pass it around, 90 bottles of beer on the wall. | ||
| 34 | |||
| 35 | 90 bottles of beer on the wall, 90 bottles of beer. | ||
| 36 | Take one down and pass it around, 89 bottles of beer on the wall. | ||
| 37 | |||
| 38 | 89 bottles of beer on the wall, 89 bottles of beer. | ||
| 39 | Take one down and pass it around, 88 bottles of beer on the wall. | ||
| 40 | |||
| 41 | 88 bottles of beer on the wall, 88 bottles of beer. | ||
| 42 | Take one down and pass it around, 87 bottles of beer on the wall. | ||
| 43 | |||
| 44 | 87 bottles of beer on the wall, 87 bottles of beer. | ||
| 45 | Take one down and pass it around, 86 bottles of beer on the wall. | ||
| 46 | |||
| 47 | 86 bottles of beer on the wall, 86 bottles of beer. | ||
| 48 | Take one down and pass it around, 85 bottles of beer on the wall. | ||
| 49 | |||
| 50 | 85 bottles of beer on the wall, 85 bottles of beer. | ||
| 51 | Take one down and pass it around, 84 bottles of beer on the wall. | ||
| 52 | |||
| 53 | 84 bottles of beer on the wall, 84 bottles of beer. | ||
| 54 | Take one down and pass it around, 83 bottles of beer on the wall. | ||
| 55 | |||
| 56 | 83 bottles of beer on the wall, 83 bottles of beer. | ||
| 57 | Take one down and pass it around, 82 bottles of beer on the wall. | ||
| 58 | |||
| 59 | 82 bottles of beer on the wall, 82 bottles of beer. | ||
| 60 | Take one down and pass it around, 81 bottles of beer on the wall. | ||
| 61 | |||
| 62 | 81 bottles of beer on the wall, 81 bottles of beer. | ||
| 63 | Take one down and pass it around, 80 bottles of beer on the wall. | ||
| 64 | |||
| 65 | 80 bottles of beer on the wall, 80 bottles of beer. | ||
| 66 | Take one down and pass it around, 79 bottles of beer on the wall. | ||
| 67 | |||
| 68 | 79 bottles of beer on the wall, 79 bottles of beer. | ||
| 69 | Take one down and pass it around, 78 bottles of beer on the wall. | ||
| 70 | |||
| 71 | 78 bottles of beer on the wall, 78 bottles of beer. | ||
| 72 | Take one down and pass it around, 77 bottles of beer on the wall. | ||
| 73 | |||
| 74 | 77 bottles of beer on the wall, 77 bottles of beer. | ||
| 75 | Take one down and pass it around, 76 bottles of beer on the wall. | ||
| 76 | |||
| 77 | 76 bottles of beer on the wall, 76 bottles of beer. | ||
| 78 | Take one down and pass it around, 75 bottles of beer on the wall. | ||
| 79 | |||
| 80 | 75 bottles of beer on the wall, 75 bottles of beer. | ||
| 81 | Take one down and pass it around, 74 bottles of beer on the wall. | ||
| 82 | |||
| 83 | 74 bottles of beer on the wall, 74 bottles of beer. | ||
| 84 | Take one down and pass it around, 73 bottles of beer on the wall. | ||
| 85 | |||
| 86 | 73 bottles of beer on the wall, 73 bottles of beer. | ||
| 87 | Take one down and pass it around, 72 bottles of beer on the wall. | ||
| 88 | |||
| 89 | 72 bottles of beer on the wall, 72 bottles of beer. | ||
| 90 | Take one down and pass it around, 71 bottles of beer on the wall. | ||
| 91 | |||
| 92 | 71 bottles of beer on the wall, 71 bottles of beer. | ||
| 93 | Take one down and pass it around, 70 bottles of beer on the wall. | ||
| 94 | |||
| 95 | 70 bottles of beer on the wall, 70 bottles of beer. | ||
| 96 | Take one down and pass it around, 69 bottles of beer on the wall. | ||
| 97 | |||
| 98 | 69 bottles of beer on the wall, 69 bottles of beer. | ||
| 99 | Take one down and pass it around, 68 bottles of beer on the wall. | ||
| 100 | |||
| 101 | 68 bottles of beer on the wall, 68 bottles of beer. | ||
| 102 | Take one down and pass it around, 67 bottles of beer on the wall. | ||
| 103 | |||
| 104 | 67 bottles of beer on the wall, 67 bottles of beer. | ||
| 105 | Take one down and pass it around, 66 bottles of beer on the wall. | ||
| 106 | |||
| 107 | 66 bottles of beer on the wall, 66 bottles of beer. | ||
| 108 | Take one down and pass it around, 65 bottles of beer on the wall. | ||
| 109 | |||
| 110 | 65 bottles of beer on the wall, 65 bottles of beer. | ||
| 111 | Take one down and pass it around, 64 bottles of beer on the wall. | ||
| 112 | |||
| 113 | 64 bottles of beer on the wall, 64 bottles of beer. | ||
| 114 | Take one down and pass it around, 63 bottles of beer on the wall. | ||
| 115 | |||
| 116 | 63 bottles of beer on the wall, 63 bottles of beer. | ||
| 117 | Take one down and pass it around, 62 bottles of beer on the wall. | ||
| 118 | |||
| 119 | 62 bottles of beer on the wall, 62 bottles of beer. | ||
| 120 | Take one down and pass it around, 61 bottles of beer on the wall. | ||
| 121 | |||
| 122 | 61 bottles of beer on the wall, 61 bottles of beer. | ||
| 123 | Take one down and pass it around, 60 bottles of beer on the wall. | ||
| 124 | |||
| 125 | 60 bottles of beer on the wall, 60 bottles of beer. | ||
| 126 | Take one down and pass it around, 59 bottles of beer on the wall. | ||
| 127 | |||
| 128 | 59 bottles of beer on the wall, 59 bottles of beer. | ||
| 129 | Take one down and pass it around, 58 bottles of beer on the wall. | ||
| 130 | |||
| 131 | 58 bottles of beer on the wall, 58 bottles of beer. | ||
| 132 | Take one down and pass it around, 57 bottles of beer on the wall. | ||
| 133 | |||
| 134 | 57 bottles of beer on the wall, 57 bottles of beer. | ||
| 135 | Take one down and pass it around, 56 bottles of beer on the wall. | ||
| 136 | |||
| 137 | 56 bottles of beer on the wall, 56 bottles of beer. | ||
| 138 | Take one down and pass it around, 55 bottles of beer on the wall. | ||
| 139 | |||
| 140 | 55 bottles of beer on the wall, 55 bottles of beer. | ||
| 141 | Take one down and pass it around, 54 bottles of beer on the wall. | ||
| 142 | |||
| 143 | 54 bottles of beer on the wall, 54 bottles of beer. | ||
| 144 | Take one down and pass it around, 53 bottles of beer on the wall. | ||
| 145 | |||
| 146 | 53 bottles of beer on the wall, 53 bottles of beer. | ||
| 147 | Take one down and pass it around, 52 bottles of beer on the wall. | ||
| 148 | |||
| 149 | 52 bottles of beer on the wall, 52 bottles of beer. | ||
| 150 | Take one down and pass it around, 51 bottles of beer on the wall. | ||
| 151 | |||
| 152 | 51 bottles of beer on the wall, 51 bottles of beer. | ||
| 153 | Take one down and pass it around, 50 bottles of beer on the wall. | ||
| 154 | |||
| 155 | 50 bottles of beer on the wall, 50 bottles of beer. | ||
| 156 | Take one down and pass it around, 49 bottles of beer on the wall. | ||
| 157 | |||
| 158 | 49 bottles of beer on the wall, 49 bottles of beer. | ||
| 159 | Take one down and pass it around, 48 bottles of beer on the wall. | ||
| 160 | |||
| 161 | 48 bottles of beer on the wall, 48 bottles of beer. | ||
| 162 | Take one down and pass it around, 47 bottles of beer on the wall. | ||
| 163 | |||
| 164 | 47 bottles of beer on the wall, 47 bottles of beer. | ||
| 165 | Take one down and pass it around, 46 bottles of beer on the wall. | ||
| 166 | |||
| 167 | 46 bottles of beer on the wall, 46 bottles of beer. | ||
| 168 | Take one down and pass it around, 45 bottles of beer on the wall. | ||
| 169 | |||
| 170 | 45 bottles of beer on the wall, 45 bottles of beer. | ||
| 171 | Take one down and pass it around, 44 bottles of beer on the wall. | ||
| 172 | |||
| 173 | 44 bottles of beer on the wall, 44 bottles of beer. | ||
| 174 | Take one down and pass it around, 43 bottles of beer on the wall. | ||
| 175 | |||
| 176 | 43 bottles of beer on the wall, 43 bottles of beer. | ||
| 177 | Take one down and pass it around, 42 bottles of beer on the wall. | ||
| 178 | |||
| 179 | 42 bottles of beer on the wall, 42 bottles of beer. | ||
| 180 | Take one down and pass it around, 41 bottles of beer on the wall. | ||
| 181 | |||
| 182 | 41 bottles of beer on the wall, 41 bottles of beer. | ||
| 183 | Take one down and pass it around, 40 bottles of beer on the wall. | ||
| 184 | |||
| 185 | 40 bottles of beer on the wall, 40 bottles of beer. | ||
| 186 | Take one down and pass it around, 39 bottles of beer on the wall. | ||
| 187 | |||
| 188 | 39 bottles of beer on the wall, 39 bottles of beer. | ||
| 189 | Take one down and pass it around, 38 bottles of beer on the wall. | ||
| 190 | |||
| 191 | 38 bottles of beer on the wall, 38 bottles of beer. | ||
| 192 | Take one down and pass it around, 37 bottles of beer on the wall. | ||
| 193 | |||
| 194 | 37 bottles of beer on the wall, 37 bottles of beer. | ||
| 195 | Take one down and pass it around, 36 bottles of beer on the wall. | ||
| 196 | |||
| 197 | 36 bottles of beer on the wall, 36 bottles of beer. | ||
| 198 | Take one down and pass it around, 35 bottles of beer on the wall. | ||
| 199 | |||
| 200 | 35 bottles of beer on the wall, 35 bottles of beer. | ||
| 201 | Take one down and pass it around, 34 bottles of beer on the wall. | ||
| 202 | |||
| 203 | 34 bottles of beer on the wall, 34 bottles of beer. | ||
| 204 | Take one down and pass it around, 33 bottles of beer on the wall. | ||
| 205 | |||
| 206 | 33 bottles of beer on the wall, 33 bottles of beer. | ||
| 207 | Take one down and pass it around, 32 bottles of beer on the wall. | ||
| 208 | |||
| 209 | 32 bottles of beer on the wall, 32 bottles of beer. | ||
| 210 | Take one down and pass it around, 31 bottles of beer on the wall. | ||
| 211 | |||
| 212 | 31 bottles of beer on the wall, 31 bottles of beer. | ||
| 213 | Take one down and pass it around, 30 bottles of beer on the wall. | ||
| 214 | |||
| 215 | 30 bottles of beer on the wall, 30 bottles of beer. | ||
| 216 | Take one down and pass it around, 29 bottles of beer on the wall. | ||
| 217 | |||
| 218 | 29 bottles of beer on the wall, 29 bottles of beer. | ||
| 219 | Take one down and pass it around, 28 bottles of beer on the wall. | ||
| 220 | |||
| 221 | 28 bottles of beer on the wall, 28 bottles of beer. | ||
| 222 | Take one down and pass it around, 27 bottles of beer on the wall. | ||
| 223 | |||
| 224 | 27 bottles of beer on the wall, 27 bottles of beer. | ||
| 225 | Take one down and pass it around, 26 bottles of beer on the wall. | ||
| 226 | |||
| 227 | 26 bottles of beer on the wall, 26 bottles of beer. | ||
| 228 | Take one down and pass it around, 25 bottles of beer on the wall. | ||
| 229 | |||
| 230 | 25 bottles of beer on the wall, 25 bottles of beer. | ||
| 231 | Take one down and pass it around, 24 bottles of beer on the wall. | ||
| 232 | |||
| 233 | 24 bottles of beer on the wall, 24 bottles of beer. | ||
| 234 | Take one down and pass it around, 23 bottles of beer on the wall. | ||
| 235 | |||
| 236 | 23 bottles of beer on the wall, 23 bottles of beer. | ||
| 237 | Take one down and pass it around, 22 bottles of beer on the wall. | ||
| 238 | |||
| 239 | 22 bottles of beer on the wall, 22 bottles of beer. | ||
| 240 | Take one down and pass it around, 21 bottles of beer on the wall. | ||
| 241 | |||
| 242 | 21 bottles of beer on the wall, 21 bottles of beer. | ||
| 243 | Take one down and pass it around, 20 bottles of beer on the wall. | ||
| 244 | |||
| 245 | 20 bottles of beer on the wall, 20 bottles of beer. | ||
| 246 | Take one down and pass it around, 19 bottles of beer on the wall. | ||
| 247 | |||
| 248 | 19 bottles of beer on the wall, 19 bottles of beer. | ||
| 249 | Take one down and pass it around, 18 bottles of beer on the wall. | ||
| 250 | |||
| 251 | 18 bottles of beer on the wall, 18 bottles of beer. | ||
| 252 | Take one down and pass it around, 17 bottles of beer on the wall. | ||
| 253 | |||
| 254 | 17 bottles of beer on the wall, 17 bottles of beer. | ||
| 255 | Take one down and pass it around, 16 bottles of beer on the wall. | ||
| 256 | |||
| 257 | 16 bottles of beer on the wall, 16 bottles of beer. | ||
| 258 | Take one down and pass it around, 15 bottles of beer on the wall. | ||
| 259 | |||
| 260 | 15 bottles of beer on the wall, 15 bottles of beer. | ||
| 261 | Take one down and pass it around, 14 bottles of beer on the wall. | ||
| 262 | |||
| 263 | 14 bottles of beer on the wall, 14 bottles of beer. | ||
| 264 | Take one down and pass it around, 13 bottles of beer on the wall. | ||
| 265 | |||
| 266 | 13 bottles of beer on the wall, 13 bottles of beer. | ||
| 267 | Take one down and pass it around, 12 bottles of beer on the wall. | ||
| 268 | |||
| 269 | 12 bottles of beer on the wall, 12 bottles of beer. | ||
| 270 | Take one down and pass it around, 11 bottles of beer on the wall. | ||
| 271 | |||
| 272 | 11 bottles of beer on the wall, 11 bottles of beer. | ||
| 273 | Take one down and pass it around, 10 bottles of beer on the wall. | ||
| 274 | |||
| 275 | 10 bottles of beer on the wall, 10 bottles of beer. | ||
| 276 | Take one down and pass it around, 9 bottles of beer on the wall. | ||
| 277 | |||
| 278 | 9 bottles of beer on the wall, 9 bottles of beer. | ||
| 279 | Take one down and pass it around, 8 bottles of beer on the wall. | ||
| 280 | |||
| 281 | 8 bottles of beer on the wall, 8 bottles of beer. | ||
| 282 | Take one down and pass it around, 7 bottles of beer on the wall. | ||
| 283 | |||
| 284 | 7 bottles of beer on the wall, 7 bottles of beer. | ||
| 285 | Take one down and pass it around, 6 bottles of beer on the wall. | ||
| 286 | |||
| 287 | 6 bottles of beer on the wall, 6 bottles of beer. | ||
| 288 | Take one down and pass it around, 5 bottles of beer on the wall. | ||
| 289 | |||
| 290 | 5 bottles of beer on the wall, 5 bottles of beer. | ||
| 291 | Take one down and pass it around, 4 bottles of beer on the wall. | ||
| 292 | |||
| 293 | 4 bottles of beer on the wall, 4 bottles of beer. | ||
| 294 | Take one down and pass it around, 3 bottles of beer on the wall. | ||
| 295 | |||
| 296 | 3 bottles of beer on the wall, 3 bottles of beer. | ||
| 297 | Take one down and pass it around, 2 bottles of beer on the wall. | ||
| 298 | |||
| 299 | 2 bottles of beer on the wall, 2 bottles of beer. | ||
| 300 | Take one down and pass it around, 1 bottle of beer on the wall. | ||
| 301 | |||
| 302 | 1 bottle of beer on the wall, 1 bottle of beer. | ||
| 303 | Take it down and pass it around, no more bottles of beer on the wall. | ||
| 304 | |||
| 305 | No more bottles of beer on the wall, no more bottles of beer. | ||
| 306 | Go to the store and buy some more, 99 bottles of beer on the wall. | ||
| 307 | ``` | ||
| 308 | |||
| 309 | ## For bonus points | ||
| 310 | |||
| 311 | Did you get the tests passing and the code clean? If you want to, these | ||
| 312 | are some additional things you could try: | ||
| 313 | |||
| 314 | * Remove as much duplication as you possibly can. | ||
| 315 | * Optimize for readability, even if it means introducing duplication. | ||
| 316 | * If you've removed all the duplication, do you have a lot of | ||
| 317 | conditionals? Try replacing the conditionals with polymorphism, if it | ||
| 318 | applies in this language. How readable is it? | ||
| 319 | |||
| 320 | Then please share your thoughts in a comment on the submission. Did this | ||
| 321 | experiment make the code better? Worse? Did you learn anything from it? | ||
| 322 | |||
| 323 | ## Rust Installation | ||
| 324 | |||
| 325 | Refer to the [exercism help page][help-page] for Rust installation and learning | ||
| 326 | resources. | ||
| 327 | |||
| 328 | ## Writing the Code | ||
| 329 | |||
| 330 | Execute the tests with: | ||
| 331 | |||
| 332 | ```bash | ||
| 333 | $ cargo test | ||
| 334 | ``` | ||
| 335 | |||
| 336 | All but the first test have been ignored. After you get the first test to | ||
| 337 | pass, open the tests source file which is located in the `tests` directory | ||
| 338 | and remove the `#[ignore]` flag from the next test and get the tests to pass | ||
| 339 | again. Each separate test is a function with `#[test]` flag above it. | ||
| 340 | Continue, until you pass every test. | ||
| 341 | |||
| 342 | If you wish to run all ignored tests without editing the tests source file, use: | ||
| 343 | |||
| 344 | ```bash | ||
| 345 | $ cargo test -- --ignored | ||
| 346 | ``` | ||
| 347 | |||
| 348 | To run a specific test, for example `some_test`, you can use: | ||
| 349 | |||
| 350 | ```bash | ||
| 351 | $ cargo test some_test | ||
| 352 | ``` | ||
| 353 | |||
| 354 | If the specific test is ignored use: | ||
| 355 | |||
| 356 | ```bash | ||
| 357 | $ cargo test some_test -- --ignored | ||
| 358 | ``` | ||
| 359 | |||
| 360 | To learn more about Rust tests refer to the [online test documentation][rust-tests] | ||
| 361 | |||
| 362 | Make sure to read the [Modules][modules] chapter if you | ||
| 363 | haven't already, it will help you with organizing your files. | ||
| 364 | |||
| 365 | ## Further improvements | ||
| 366 | |||
| 367 | After you have solved the exercise, please consider using the additional utilities, described in the [installation guide](https://exercism.io/tracks/rust/installation), to further refine your final solution. | ||
| 368 | |||
| 369 | To format your solution, inside the solution directory use | ||
| 370 | |||
| 371 | ```bash | ||
| 372 | cargo fmt | ||
| 373 | ``` | ||
| 374 | |||
| 375 | To see, if your solution contains some common ineffective use cases, inside the solution directory use | ||
| 376 | |||
| 377 | ```bash | ||
| 378 | cargo clippy --all-targets | ||
| 379 | ``` | ||
| 380 | |||
| 381 | ## Submitting the solution | ||
| 382 | |||
| 383 | 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. | ||
| 384 | |||
| 385 | ## Feedback, Issues, Pull Requests | ||
| 386 | |||
| 387 | The [exercism/rust](https://github.com/exercism/rust) repository on 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! | ||
| 388 | |||
| 389 | If you want to know more about Exercism, take a look at the [contribution guide](https://github.com/exercism/docs/blob/master/contributing-to-language-tracks/README.md). | ||
| 390 | |||
| 391 | [help-page]: https://exercism.io/tracks/rust/learning | ||
| 392 | [modules]: https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html | ||
| 393 | [cargo]: https://doc.rust-lang.org/book/ch14-00-more-about-cargo.html | ||
| 394 | [rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html | ||
| 395 | |||
| 396 | ## Source | ||
| 397 | |||
| 398 | Learn to Program by Chris Pine [http://pine.fm/LearnToProgram/?Chapter=06](http://pine.fm/LearnToProgram/?Chapter=06) | ||
| 399 | |||
| 400 | ## Submitting Incomplete Solutions | ||
| 401 | It's possible to submit an incomplete solution so you can see how others have completed the exercise. | ||
diff --git a/rust/beer-song/src/intersperse.rs b/rust/beer-song/src/intersperse.rs new file mode 100644 index 0000000..51580e6 --- /dev/null +++ b/rust/beer-song/src/intersperse.rs | |||
| @@ -0,0 +1,52 @@ | |||
| 1 | use std::rc::Rc; | ||
| 2 | |||
| 3 | /// An iterator that allows to insert a given element `elem` every `interval` elements of the input | ||
| 4 | /// iterator. | ||
| 5 | /// | ||
| 6 | /// The interleaved element is wrapped in a `Rc` smart pointer to avoid hard copies. | ||
| 7 | pub struct Intersperse<I: Iterator> { | ||
| 8 | inner: std::iter::Peekable<I>, // required to have `peek(..)` | ||
| 9 | elem: Rc<I::Item>, | ||
| 10 | interval: u32, | ||
| 11 | intersperse: u32, | ||
| 12 | } | ||
| 13 | |||
| 14 | impl<I: Iterator> Intersperse<I> { | ||
| 15 | pub fn new(inner: I, elem: I::Item, interval: u32) -> Intersperse<I> { | ||
| 16 | Intersperse { | ||
| 17 | inner: inner.peekable(), | ||
| 18 | elem: Rc::new(elem), | ||
| 19 | interval: interval + 1, // `+ 1` needed to align semantics and implementation | ||
| 20 | intersperse: 0, | ||
| 21 | } | ||
| 22 | } | ||
| 23 | } | ||
| 24 | |||
| 25 | impl<I: Iterator> Iterator for Intersperse<I> { | ||
| 26 | type Item = Rc<I::Item>; | ||
| 27 | |||
| 28 | fn next(&mut self) -> Option<Self::Item> { | ||
| 29 | // Counter rotation | ||
| 30 | self.intersperse = (self.intersperse + 1) % self.interval; | ||
| 31 | // Insert a copy of the element only when counter goes to 0 and we haven't reached the end | ||
| 32 | // of the inner iterator. | ||
| 33 | if self.intersperse == 0 && self.inner.peek().is_some() { | ||
| 34 | Some(Rc::clone(&self.elem)) | ||
| 35 | } else { | ||
| 36 | self.inner.next() | ||
| 37 | .map(|x| Rc::new(x)) // Go from Option<I::Item> to Option<Rc<I::Item>> | ||
| 38 | } | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | /// Extend `Iterator`s with `intersperse` function. | ||
| 43 | pub trait IntersperseIterator: Iterator { | ||
| 44 | fn intersperse(self, elem: Self::Item, interval: u32) -> Intersperse<Self> | ||
| 45 | where | ||
| 46 | Self: Sized, | ||
| 47 | { | ||
| 48 | Intersperse::new(self, elem, interval) | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | impl<I: Iterator> IntersperseIterator for I {} | ||
diff --git a/rust/beer-song/src/lib.rs b/rust/beer-song/src/lib.rs new file mode 100644 index 0000000..990fbbf --- /dev/null +++ b/rust/beer-song/src/lib.rs | |||
| @@ -0,0 +1,20 @@ | |||
| 1 | use itertools::Itertools; | ||
| 2 | |||
| 3 | pub fn verse(n: u32) -> String { | ||
| 4 | match n { | ||
| 5 | 0 => format!( "No more bottles of beer on the wall, no more bottles of beer.\n\ | ||
| 6 | Go to the store and buy some more, 99 bottles of beer on the wall.\n"), | ||
| 7 | 1 => format!( "1 bottle of beer on the wall, 1 bottle of beer.\n\ | ||
| 8 | Take it down and pass it around, no more bottles of beer on the wall.\n"), | ||
| 9 | 2 => format!( "2 bottles of beer on the wall, 2 bottles of beer.\n\ | ||
| 10 | Take one down and pass it around, 1 bottle of beer on the wall.\n"), | ||
| 11 | _ => format!( "{} bottles of beer on the wall, {} bottles of beer.\n\ | ||
| 12 | Take one down and pass it around, {} bottles of beer on the wall.\n", | ||
| 13 | n, n, n - 1) | ||
| 14 | } | ||
| 15 | } | ||
| 16 | |||
| 17 | pub fn sing(start: u32, end: u32) -> String { | ||
| 18 | // Note: call to `join` can be substituted with `.intersperse(String::from("\n")).collect()` | ||
| 19 | (end..=start).rev().map(|x| verse(x)).join("\n") | ||
| 20 | } | ||
diff --git a/rust/beer-song/tests/beer-song.rs b/rust/beer-song/tests/beer-song.rs new file mode 100644 index 0000000..2f2ad11 --- /dev/null +++ b/rust/beer-song/tests/beer-song.rs | |||
| @@ -0,0 +1,31 @@ | |||
| 1 | use beer_song as beer; | ||
| 2 | |||
| 3 | #[test] | ||
| 4 | fn test_verse_0() { | ||
| 5 | assert_eq!(beer::verse(0), "No more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.\n"); | ||
| 6 | } | ||
| 7 | |||
| 8 | #[test] | ||
| 9 | fn test_verse_1() { | ||
| 10 | assert_eq!(beer::verse(1), "1 bottle of beer on the wall, 1 bottle of beer.\nTake it down and pass it around, no more bottles of beer on the wall.\n"); | ||
| 11 | } | ||
| 12 | |||
| 13 | #[test] | ||
| 14 | fn test_verse_2() { | ||
| 15 | assert_eq!(beer::verse(2), "2 bottles of beer on the wall, 2 bottles of beer.\nTake one down and pass it around, 1 bottle of beer on the wall.\n"); | ||
| 16 | } | ||
| 17 | |||
| 18 | #[test] | ||
| 19 | fn test_verse_8() { | ||
| 20 | assert_eq!(beer::verse(8), "8 bottles of beer on the wall, 8 bottles of beer.\nTake one down and pass it around, 7 bottles of beer on the wall.\n"); | ||
| 21 | } | ||
| 22 | |||
| 23 | #[test] | ||
| 24 | fn test_song_8_6() { | ||
| 25 | assert_eq!(beer::sing(8, 6), "8 bottles of beer on the wall, 8 bottles of beer.\nTake one down and pass it around, 7 bottles of beer on the wall.\n\n7 bottles of beer on the wall, 7 bottles of beer.\nTake one down and pass it around, 6 bottles of beer on the wall.\n\n6 bottles of beer on the wall, 6 bottles of beer.\nTake one down and pass it around, 5 bottles of beer on the wall.\n"); | ||
| 26 | } | ||
| 27 | |||
| 28 | #[test] | ||
| 29 | fn test_song_3_0() { | ||
| 30 | assert_eq!(beer::sing(3, 0), "3 bottles of beer on the wall, 3 bottles of beer.\nTake one down and pass it around, 2 bottles of beer on the wall.\n\n2 bottles of beer on the wall, 2 bottles of beer.\nTake one down and pass it around, 1 bottle of beer on the wall.\n\n1 bottle of beer on the wall, 1 bottle of beer.\nTake it down and pass it around, no more bottles of beer on the wall.\n\nNo more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.\n"); | ||
| 31 | } | ||
