Introduction to the Cargo Package Manager
Using external packages is a feature of the Rust language. For example, to use the Rand crate, you need to modify the Cargo.toml file before compiling code. Add the following code lines to the end of the file:
[dependencies] rand = "0.3.0"
In this example, rand = 0.3.0 is used, that is, rand= ^0.3.0, indicating any version compatible with 0.3.0, that is, rand=0.3.x. During compilation, the latest version is obtained by default, for example, 0.3.14. If a version must be used, for example, 0.3.0, run the following command:
[dependencies] rand = "=0.3.0"
In addition, during the first compilation, Cargo finds out all the required versions and writes them to the Cargo.lock file. During subsequent project build, Cargo will notice the existence of Cargo.lock and directly use the specified version without checking the version information.
When compiling Rust code, the downloaded crate is saved in the ~/.Cargo directory by default. If the code in this directory has a change, the new crate will be automatically downloaded to overwrite the existing one. Therefore, if you want to modify the crate code, use the following code to describe the dependency on the local crate:
[patch.crates-io]
rand = { path = "./rand"}