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.
1 2 3 4 5 6 7 8 9 10 11 12 | [package] name = "rust-turreta-simple-rust" version = "0.1.0" authors = ["Karl San Gabriel"] edition = "2018" [dependencies] uuid = { version = "0.8.1", features = ["serde", "v4"] } [dev-dependencies] mockers = "0.21.0" mockers_derive = "0.21.0" |
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.
1 2 3 4 5 6 7 8 9 10 11 | [package] name = "dev-dep-dep" version = "0.1.0" authors = ["Karl San Gabriel"] edition = "2018" [dependencies] actix-web = "2.0.0" actix-rt = "1.1.1" [dev-dependencies] |
1 2 3 | fn main() { println!("Hello, world!"); } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | C:\Users\karldev\Desktop\dev\blogs\rust\dev-dep-dep\target\debug>dir Volume in drive C is Windows Volume Serial Number is 14A2-F668 Directory of C:\Users\karldev\Desktop\dev\blogs\rust\dev-dep-dep\target\debug 07/05/2020 01:23 PM <DIR> . 07/05/2020 01:23 PM <DIR> .. 07/05/2020 01:21 PM 0 .cargo-lock 07/05/2020 01:21 PM <DIR> .fingerprint 07/05/2020 01:21 PM <DIR> build 07/05/2020 01:23 PM <DIR> deps 07/05/2020 01:23 PM 146 dev-dep-dep.d 07/05/2020 01:23 PM 155,136 dev-dep-dep.exe 07/05/2020 01:21 PM <DIR> examples 07/05/2020 01:23 PM <DIR> incremental 3 File(s) 155,282 bytes 7 Dir(s) 300,216,774,656 bytes free |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | C:\Users\karldev\Desktop\dev\blogs\rust\dev-dep-dep\target\release>dir Volume in drive C is Windows Volume Serial Number is 14A2-F668 Directory of C:\Users\karldev\Desktop\dev\blogs\rust\dev-dep-dep\target\release 07/05/2020 01:33 PM <DIR> . 07/05/2020 01:33 PM <DIR> .. 07/05/2020 01:27 PM 0 .cargo-lock 07/05/2020 01:27 PM <DIR> .fingerprint 07/05/2020 01:27 PM <DIR> build 07/05/2020 01:33 PM <DIR> deps 07/05/2020 01:33 PM 148 dev-dep-dep.d 07/05/2020 01:33 PM 152,064 dev-dep-dep.exe 07/05/2020 01:27 PM <DIR> examples 07/05/2020 01:27 PM <DIR> incremental 3 File(s) 152,212 bytes 7 Dir(s) 299,777,642,496 bytes free |
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.
1 2 3 4 5 6 7 8 9 10 11 | [package] name = "dev-dep-dep" version = "0.1.0" authors = ["Karl San Gabriel"] edition = "2018" [dependencies] actix-web = "2.0.0" [dev-dependencies] actix-rt = "1.1.1" |
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.
1 2 3 4 | C:\Users\karldev\Desktop\dev\blogs\rust\dev-dep-dep>cargo tree -e dev dev-dep-dep v0.1.0 (C:\Users\karldev\Desktop\dev\blogs\rust\dev-dep-dep) [dev-dependencies] └── actix-rt v1.1.1 |
To display non-dev dependencies, use cargo tree -e normal.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | C:\Users\karldev\Desktop\dev\blogs\rust\dev-dep-dep>cargo tree -e normal dev-dep-dep v0.1.0 (C:\Users\karldev\Desktop\dev\blogs\rust\dev-dep-dep) └── actix-web v2.0.0 ├── actix-codec v0.2.0 │ ├── bitflags v1.2.1 │ ├── bytes v0.5.5 │ ├── futures-core v0.3.5 │ ├── futures-sink v0.3.5 │ ├── log v0.4.8 │ │ └── cfg-if v0.1.10 │ ├── tokio v0.2.21 │ │ ├── bytes v0.5.5 │ │ ├── futures-core v0.3.5 │ │ ├── iovec v0.1.4 ... |