2018-03-04 17:58:40 +00:00
|
|
|
<!--
|
|
|
|
{{ autogenerated_notice }}
|
|
|
|
-->
|
|
|
|
|
2018-03-04 17:41:55 +00:00
|
|
|
# rustlings
|
|
|
|
|
2018-03-04 18:42:44 +00:00
|
|
|
Small exercises to get you used to reading and writing Rust code. Includes practice reading and
|
|
|
|
responding to compiler messages!
|
2018-03-04 17:41:55 +00:00
|
|
|
|
|
|
|
This repo is very much the smallest thing that could possibly work :)
|
|
|
|
|
|
|
|
## To do these exercises
|
|
|
|
|
2018-03-04 18:42:44 +00:00
|
|
|
Thanks to [btbytes'](https://twitter.com/btbytes) [prlinks](https://github.com/btbytes/prlink), you
|
|
|
|
can now click on the links below to load the exercises in the rust playground!
|
2018-03-04 17:41:55 +00:00
|
|
|
|
2018-03-04 18:42:44 +00:00
|
|
|
There are infinite correct answers-- the exercises are sometimes left very open-ended. Scroll down
|
|
|
|
in the playground to find comments that have hints.
|
2018-03-04 17:41:55 +00:00
|
|
|
|
2018-03-04 18:42:44 +00:00
|
|
|
If you need more help or would like to compare solutions, you can ask in [#rust-beginners on
|
|
|
|
irc.mozilla.org](https://chat.mibbit.com/?server=irc.mozilla.org&channel=%23rust-beginners ), the
|
|
|
|
[user forum](https://users.rust-lang.org/), or [the subreddit](https://reddit.com/r/rust). If an
|
|
|
|
exercise could be improved in any way, please [create an
|
|
|
|
issue](https://github.com/carols10cents/rustlings/issues/new) or submit a pull request!
|
2018-03-04 17:41:55 +00:00
|
|
|
|
|
|
|
### Variable bindings
|
|
|
|
|
2018-03-04 18:42:44 +00:00
|
|
|
[Relevant chapter in The Rust Programming
|
2018-03-04 21:31:07 +00:00
|
|
|
Language](https://doc.rust-lang.org/book/second-edition/ch03-01-variables-and-mutability.html)
|
2018-03-04 17:41:55 +00:00
|
|
|
|
2018-03-04 19:02:20 +00:00
|
|
|
{{ playground_link "variables/variables1.rs" }}
|
|
|
|
{{ playground_link "variables/variables2.rs" }}
|
|
|
|
{{ playground_link "variables/variables3.rs" }}
|
|
|
|
{{ playground_link "variables/variables4.rs" }}
|
2018-03-04 17:41:55 +00:00
|
|
|
|
|
|
|
### Functions
|
|
|
|
|
2018-03-04 18:42:44 +00:00
|
|
|
[Relevant chapter in The Rust Programming
|
2018-03-04 21:31:07 +00:00
|
|
|
Language](https://doc.rust-lang.org/book/second-edition/ch03-03-how-functions-work.html)
|
2018-03-04 17:41:55 +00:00
|
|
|
|
2018-03-04 19:02:20 +00:00
|
|
|
{{ playground_link "functions/functions1.rs" }}
|
|
|
|
{{ playground_link "functions/functions2.rs" }}
|
|
|
|
{{ playground_link "functions/functions3.rs" }}
|
|
|
|
{{ playground_link "functions/functions4.rs" }}
|
|
|
|
{{ playground_link "functions/functions5.rs" }}
|
2018-03-04 17:41:55 +00:00
|
|
|
|
|
|
|
### Primitive types
|
|
|
|
|
2018-03-04 18:42:44 +00:00
|
|
|
[Relevant chapter in The Rust Programming
|
2018-03-04 21:31:07 +00:00
|
|
|
Language](https://doc.rust-lang.org/book/second-edition/ch03-02-data-types.html)
|
2018-03-04 17:41:55 +00:00
|
|
|
|
2018-03-04 19:02:20 +00:00
|
|
|
{{ playground_link "primitive_types/primitive_types1.rs" }}
|
|
|
|
{{ playground_link "primitive_types/primitive_types2.rs" }}
|
|
|
|
{{ playground_link "primitive_types/primitive_types3.rs" }}
|
|
|
|
{{ playground_link "primitive_types/primitive_types4.rs" }}
|
|
|
|
{{ playground_link "primitive_types/primitive_types5.rs" }}
|
|
|
|
{{ playground_link "primitive_types/primitive_types6.rs" }}
|
2018-03-04 17:41:55 +00:00
|
|
|
|
|
|
|
### Tests
|
|
|
|
|
2018-03-04 21:31:07 +00:00
|
|
|
Going out of order from the book to cover tests-- many of the following exercises will ask you to
|
|
|
|
make tests pass!
|
2018-03-04 17:41:55 +00:00
|
|
|
|
2018-03-04 21:31:07 +00:00
|
|
|
[Relevant chapter in The Rust Programming
|
|
|
|
Language](https://doc.rust-lang.org/book/second-edition/ch11-01-writing-tests.html)
|
2018-03-04 17:41:55 +00:00
|
|
|
|
2018-03-04 19:02:20 +00:00
|
|
|
{{ playground_link "tests/tests1.rs" }}
|
|
|
|
{{ playground_link "tests/tests2.rs" }}
|
|
|
|
{{ playground_link "tests/tests3.rs" }}
|
|
|
|
{{ playground_link "tests/tests4.rs" }}
|
2018-03-04 17:41:55 +00:00
|
|
|
|
|
|
|
### If
|
|
|
|
|
2018-03-04 18:42:44 +00:00
|
|
|
[Relevant chapter in The Rust Programming
|
2018-03-04 21:31:07 +00:00
|
|
|
Language](https://doc.rust-lang.org/book/second-edition/ch03-05-control-flow.html)
|
2018-03-04 17:41:55 +00:00
|
|
|
|
2018-03-04 19:02:20 +00:00
|
|
|
{{ playground_link "if/if1.rs" }}
|
2018-03-04 17:41:55 +00:00
|
|
|
|
|
|
|
### Strings
|
|
|
|
|
2018-03-04 18:42:44 +00:00
|
|
|
[Relevant chapter in The Rust Programming
|
2018-03-04 21:31:07 +00:00
|
|
|
Language](https://doc.rust-lang.org/book/second-edition/ch08-02-strings.html)
|
2018-03-04 17:41:55 +00:00
|
|
|
|
2018-03-04 19:02:20 +00:00
|
|
|
{{ playground_link "strings/strings1.rs" }}
|
|
|
|
{{ playground_link "strings/strings2.rs" }}
|
|
|
|
{{ playground_link "strings/strings3.rs" }}
|
2018-03-04 17:41:55 +00:00
|
|
|
|
|
|
|
### Move semantics
|
|
|
|
|
2018-03-04 18:42:44 +00:00
|
|
|
These exercises are adapted from [pnkfelix]()'s [Rust
|
|
|
|
Tutorial](https://pnkfelix.github.io/rust-examples-icfp2014/) -- thank you Felix!!!
|
2018-03-04 17:41:55 +00:00
|
|
|
|
|
|
|
Relevant chapters in the book:
|
2018-03-04 18:42:44 +00:00
|
|
|
|
2018-03-04 21:31:07 +00:00
|
|
|
- [Ownership](https://doc.rust-lang.org/book/second-edition/ch04-01-what-is-ownership.html)
|
|
|
|
- [References and borrowing](https://doc.rust-lang.org/book/second-edition/ch04-02-references-and-borrowing.html)
|
2018-03-04 17:41:55 +00:00
|
|
|
|
2018-03-04 18:42:44 +00:00
|
|
|
Note that the exercises in this section may look similar to each other but they are subtly
|
|
|
|
different :)
|
2018-03-04 17:41:55 +00:00
|
|
|
|
2018-03-04 19:02:20 +00:00
|
|
|
{{ playground_link "move_semantics/move_semantics1.rs" }}
|
|
|
|
{{ playground_link "move_semantics/move_semantics2.rs" }}
|
|
|
|
{{ playground_link "move_semantics/move_semantics3.rs" }}
|
|
|
|
{{ playground_link "move_semantics/move_semantics4.rs" }}
|
2018-03-04 17:41:55 +00:00
|
|
|
|
|
|
|
### Modules
|
|
|
|
|
2018-03-04 18:42:44 +00:00
|
|
|
[Relevant chapter in The Rust Programming
|
2018-03-04 21:31:07 +00:00
|
|
|
Language](https://doc.rust-lang.org/book/second-edition/ch07-01-mod-and-the-filesystem.html)
|
2018-03-04 17:41:55 +00:00
|
|
|
|
2018-03-04 19:02:20 +00:00
|
|
|
{{ playground_link "modules/modules1.rs" }}
|
|
|
|
{{ playground_link "modules/modules2.rs" }}
|
2018-03-04 17:41:55 +00:00
|
|
|
|
|
|
|
### Macros
|
|
|
|
|
2018-03-04 21:31:07 +00:00
|
|
|
Check out:
|
|
|
|
|
|
|
|
- [The Macros section of the first edition of the book
|
|
|
|
book](https://doc.rust-lang.org/book/first-edition/macros.html)
|
|
|
|
- [The Macros appendix of the second edition of the
|
|
|
|
book](https://doc.rust-lang.org/book/second-edition/appendix-04-macros.html)
|
|
|
|
- [The Little Book of Rust Macros](https://danielkeep.github.io/tlborm/book/index.html)
|
2018-03-04 17:41:55 +00:00
|
|
|
|
2018-03-04 19:02:20 +00:00
|
|
|
{{ playground_link "macros/macros1.rs" }}
|
|
|
|
{{ playground_link "macros/macros2.rs" }}
|
|
|
|
{{ playground_link "macros/macros3.rs" }}
|
|
|
|
{{ playground_link "macros/macros4.rs" }}
|
2018-03-04 17:41:55 +00:00
|
|
|
|
|
|
|
### Error Handling
|
|
|
|
|
2018-03-04 21:31:07 +00:00
|
|
|
The [Error
|
|
|
|
Handling](https://doc.rust-lang.org/book/second-edition/ch09-02-recoverable-errors-with-result.html)
|
|
|
|
and [Generics](https://doc.rust-lang.org/book/second-edition/ch10-01-syntax.html) sections are
|
|
|
|
relevant.
|
2018-03-04 17:41:55 +00:00
|
|
|
|
2018-03-04 19:02:20 +00:00
|
|
|
{{ playground_link "error_handling/option1.rs" }}
|
|
|
|
{{ playground_link "error_handling/result1.rs" }}
|
|
|
|
{{ playground_link "error_handling/errors1.rs" }}
|
|
|
|
{{ playground_link "error_handling/errors2.rs" }}
|
|
|
|
{{ playground_link "error_handling/errors3.rs" }}
|
|
|
|
{{ playground_link "error_handling/errorsn.rs" }}
|
2018-03-04 17:41:55 +00:00
|
|
|
|
|
|
|
### Standard library types
|
|
|
|
|
|
|
|
#### `Arc`
|
|
|
|
|
2018-03-04 21:31:07 +00:00
|
|
|
The [Concurrency](https://doc.rust-lang.org/book/second-edition/ch16-03-shared-state.html) section
|
|
|
|
is relevant.
|
2018-03-04 17:41:55 +00:00
|
|
|
|
2018-03-04 19:02:20 +00:00
|
|
|
{{ playground_link "standard_library_types/arc1.rs" }}
|
2018-03-04 17:41:55 +00:00
|
|
|
|
|
|
|
#### Iterators
|
|
|
|
|
2018-03-04 18:42:44 +00:00
|
|
|
Do not adjust your monitors-- iterators 1 and 2 are indeed missing. Iterator 3 is a bit challenging
|
|
|
|
so we're leaving space for some exercises to lead up to it!
|
2018-03-04 17:41:55 +00:00
|
|
|
|
2018-03-04 18:42:44 +00:00
|
|
|
Check out the [Iterators chapter of the
|
2018-03-04 21:31:07 +00:00
|
|
|
book](https://doc.rust-lang.org/book/second-edition/ch13-02-iterators.html) and the [Iterator
|
2018-03-04 18:42:44 +00:00
|
|
|
docs](https://doc.rust-lang.org/stable/std/iter/trait.Iterator.html).
|
2018-03-04 17:41:55 +00:00
|
|
|
|
2018-03-04 19:02:20 +00:00
|
|
|
{{ playground_link "standard_library_types/iterator3.rs" }}
|
|
|
|
{{ playground_link "standard_library_types/iterators4.rs" }}
|
2018-03-04 17:41:55 +00:00
|
|
|
|
|
|
|
### Threads
|
|
|
|
|
2018-03-04 18:42:44 +00:00
|
|
|
See [the Dining Philosophers
|
2018-10-09 18:28:54 +00:00
|
|
|
example](https://doc.rust-lang.org/1.4.0/book/dining-philosophers.html) and the
|
2018-03-04 21:31:07 +00:00
|
|
|
[Concurrency Chapter](https://doc.rust-lang.org/book/second-edition/ch16-01-threads.html) from the
|
|
|
|
book.
|
2018-03-04 17:41:55 +00:00
|
|
|
|
2018-03-04 19:02:20 +00:00
|
|
|
{{ playground_link "threads/threads1.rs" }}
|
2018-03-04 17:41:55 +00:00
|
|
|
|
|
|
|
### Uncategorized
|
|
|
|
|
|
|
|
A few exercises based on things I've encountered or had trouble with getting used to.
|
|
|
|
|
2018-03-04 19:02:20 +00:00
|
|
|
{{ playground_link "ex1.rs" }}
|
|
|
|
{{ playground_link "ex2.rs" }}
|
|
|
|
{{ playground_link "ex3.rs" }}
|
|
|
|
{{ playground_link "ex4.rs" }}
|
|
|
|
{{ playground_link "ex5.rs" }}
|
2018-03-04 17:41:55 +00:00
|
|
|
|
|
|
|
## To help with this repo/TODO list
|
|
|
|
|
|
|
|
* File issues for problems or suggestions!
|
2018-03-04 18:42:44 +00:00
|
|
|
* Contribute more exercises! Anything that took you time to get used to, or that you had trouble
|
|
|
|
with, or that deserves practice would be a good exercise!
|
|
|
|
* How could the process of doing these exercises work better? This is an open-ended question :) Are
|
|
|
|
the playground links good enough? Are there ways that we could make going to the next exercise
|
|
|
|
easier without forking the playground??
|