Learn how to test your Rust exercises on Exercism
Write your code in src/lib.rs
.
The exercises come with a stub file in src/lib.rs
that will show you the signatures of the code you'll need to write.
For most exercises, it is best to write all your code in the file src/lib.rs
.
If you would like to split your solution into several files, consult the Rust book's chapter on modules.
To run the tests, all you need to do is run the following command:
$ cargo test
Only the first test is enabled by default.
After you are ready to pass the next test, remove the ignore flag from the next test (#[ignore]
).
You can also remove the flag from all the tests at once if you prefer.
Feel free to write as little code as possible to get the tests to pass. The test failures will guide you to what should be written next.
Because Rust checks all code at compile time, you may find that your tests won't compile until you write the required code.
Even ignore
d tests are checked at compile time.
You can comment out tests that won't compile by starting each line with a //
.
Then, when you're ready to work on that test, you can un-comment it.
Rust also has a special macro called todo!()
, which you can use on unfinished code paths to make your program compile.
You should be able to solve most exercises without using any external libraries.
Nevertheless, you can add libraries (the Rust community calls them crates) by running cargo add crate-name
in the directory of an exercise.
The automatic tests on the website only support a predefined set of crates, which can be found here under the section [dependencies]
.
Feel free to open an issue or pull request if you would like support for a specific crate to be added.