O'Reilly logo

Mastering Rust by Vesa Kaihlavirta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Running tests - cargo test

Unit testing is one of the rare silver bullets of our industry that can make all the difference in keeping up high software quality. Rust supports unit testing and benchmark testing natively. Let's build a small library project to see how testing works from Cargo's side:

As you see, a library project is very similar to a binary project. The difference is that instead of main.rs and a main function inside it as an entry point, there is lib.rs with functions. Since Cargo has already created for us a skeleton lib.rs with a dummy unit test, we can run the tests right away:

Let's try a bit of test-driven development ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required