Rust, Software Development

Rust dev-dependencies and dependencies

Rust Lang

In any codebase, some codes are not for the end-users. Meaning, they do not become part of the final application – be it for QA or production environment. They are codes for testing (e.g., unit tests) and building the app (or library). If there are codes that do not finally go to the end-users, there are also libraries.  We can have a Rust project third-party libraries end up in the final build using dependencies, and the excluded ones are specified under the dev-dependencies section of our Cargo.toml file.

Using Cargo For Dependencies

We use the dev-dependencies and dependencies sections in our Cargo.toml file. Consider the following file.

When we build our project, the final application will not have the mockers and mockers_derive libraries linked to it.

Rust Dependencies For Final Application

Adding unnecessary dependencies does not affect the file size of the final Rust application when we do not use them. Consider this simple application that does not need actix-web and actix-rt crates.

In the same token, adding dependencies in the dev-dependencies does not affect the size of the Rust application.

When we build the codes in Rust using Cargo with the debug and release flags, we get the following.

The executable file is still small (at 150KB) after using either build flags.

Rust Dev Dependencies For Developers

For Rust dependencies that do not go to the executable file or library, we specify them under the dev-dependencies instead of dependencies. Consider the following Cargo.toml.

We have actix-web and actix-rt in dependencies and dev-dependencies sections, respectively.

Using Cargo Tree

When we have dependencies in both dependencies and dev-dependencies sections, the output from the Cargo’s built-in tree command can be too verbose. We can use the command’s  -e ( --edges ) flag to choose which set of dependencies to list out.

To display dev dependencies, use cargo tree -e dev.

To display non-dev dependencies, use cargo tree -e normal.

You Might Also Like