Rush’s package management tool — Cargo


Cargo is a built-in build system and package manager in the rust tool chain. It can deal with many trivial but important tasks, such as building code, downloading and compiling dependency library, etc. Most rust users will choose it to manage their own rust projects.

Create project

Managing rust projects and their module dependencies

Check cargo version

$ cargo --version

Create a project

$ cargo new hello_cargo

After executing the above command, cargo will create a project directory with the same name and place the files it generates. get intohello_cargoFolder, you can see two files and a directory just generated by cargo: one namedCargo.tomlAnd a file namedmain.rsThe source code file is placed in the SRC directory. At the same time, cargo also initializes a new git repository and generates the default git repository.gitignorefile


cargo.tlml Is the generated standard configuration file, the content is as follows:

name = "hello_cargo"
version = "0.1.0"
authors = ["Your Name <[email protected]"]
edition = "2018"


In the last line of text[dependencies]Is a region label that indicates that subsequent regions will be used to declare project dependencies.

Cargo will save all source code files to SRC directory by default, while the project root directory is only used to store files unrelated to source code, such as readme documents, license statements, configuration files, etc.

Build and run projects


In the currenthello_cargoUnder the project directory, compile:

$ cargo build
   Compiling hello_cargo v0.1.0 (/Users/Herbert/rust/projects/hello_cargo)
    Finished dev [unoptimized + debuginfo] target(s) in 0.21s

The executable is generated in the pathtarget/debug/hello_cargo

First use commandcargo buildWhen it builds, it also creates a file namedCargo.lockThis file records the specific version numbers of all the dependent libraries of the current project.

It can also be used simplycargo runCommand to complete the compilation and running tasks in turn

$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/debug/hello_cargo`
Hello, world!

If the source code is not modified, the generated binary executable will be run directly.

Compile check

adoptcargo checkTo quickly check whether the current code can be compiled without spending extra time to actually generate an executable program

$ cargo check
    Checking hello_cargo v0.1.0 (/Users/Herbert/rust/projects/hello_cargo)
    Finished dev [unoptimized + debuginfo] target(s) in 0.06s

cargo checkSkipping the step of building an executable. In the process of coding, you need to check errors through the compiler, so usecargo checkWill greatly speed up the process of compiling.

Build with release mode

When you are ready to publish your project, you can use the commandcargo build --releaseBuild and generate executable program in optimization mode

$ cargo build --release
   Compiling hello_cargo v0.1.0 (/Users/Herbert/rust/projects/hello_cargo)
    Finished release [optimized] target(s) in 0.15s

The generated executable is placed in thetarget/releaseUnder the directory.

This mode will optimize the code at the cost of longer compilation time, so that the code has better runtime performance.

Dependency package management

Update dependency

If you build a program that relies on a third-party library, you need to modify itCargo.tomlAdd a dependency. For example, the program introduces the RAND package, which is generated in cargo[dependencies]Add dependency below area:

rand = "0.3.14"

there[dependencies]The region is used to declare all the dependent packages and their version numbers needed in the project. We specify the RAND package version number as 0.3.14, where the number 0.3.14 is actually a shorthand for ^ 0.3.14, which means “any version compatible with 0.3.14 public API”.

With the dependency, cargo gets the latest version information of all available libraries from the registry, which is usually obtained from thecrates.ioIt’s a copy from the library. It will start item by item checking after updating the registry[dependencies]And download the currently missing dependency package.

Cargo.lock File lock

When building a project for the first time, cargo will traverse the declared dependencies and their corresponding semantic versions in turn, find the specific version numbers that meet the requirements, and write them into theCargo.lockDocuments.

When you build the project again, cargo will retrieve it firstCargo.lockIf there is a dependency library with a specific version specified in the file, it will skip the process of calculating the version number and directly use the version specified in the file.

Upgrade dependency package

Cargo provides a special command: update, which forces cargo to ignoreCargo.lockFile, and recalculate all the dependent packagesCargo.tomlThe latest version of the declaration. If it runs successfully, cargo will write the updated version number toCargo.lockFile and overlay the previous content.

$ cargo update
    Updating registry ``
    Updating rand v0.3.14 -> v0.3.15

reference resources

Recommended Today

Pandas data analysis — detailed explanation of super easy to use groupby

WeChat official account: “Python reads money”If there are any questions or suggestions, please official account message. In the daily data analysis, it is often necessary to analyze the dataDivide into different groups according to one (more) fieldFor example, in the field of e-commerce, the total sales of the whole country are divided by provinces, and […]