Rust Ability Development Series: Cargo and Crates Project Management (Part 1)

image

 

Preface

 

In the previous article, we talked about

 

  • Package manager

  • Module

 

These contents are the basis of project management. We know that when the project volume becomes huge, the code is usually divided into units that are easier to manage, such as modules or libraries; at the same time, it becomes necessary to build detailed engineering documents; in addition, in order to ensure the language ecosystem For sustainable development, online registration or standard setting has also become a need.

 

Then, Cargo appeared to meet these needs, and this URL: https://crates.io is used to store and share libraries licensed by the Rust community for developers to use. Need to mention again, in Rust, crate means library or package .

 

Generally speaking, a crate has three sources supported by Cargo:

  • Local directory

  • Online Git repository

    Such as github, bitbucket

  • create registry

    Such as crates.io

 

Then the content of this article will involve:

  • Create Cargo project

  • Cargo and dependencies

 

You can type Cargo on the command line and look at the related commands. Next, we will create a project with Cargo

image

 

Create a Cargo project

 

Here we first use cargo help new to see the usage of cargo new.

image

 

By default, cargo new will create a binary project. It must have the --lib parameter. Let us enter cargo new imgtool to see the directory structure:

image

image

image

 

Cargo creates some initial files: Cargo.toml and src/main.rs (send a hello world). Generally speaking, for binary crates (executable files), Cargo creates a src/main.rs file and src/lib.

Moreover, Cargo uses the usual default values ​​to initialize the Git library of the new project, which prevents the use of the .gitignore file to check the target directory and the Cargo.loc binary crates file and ignore them in the library.

 

The current default version control system is Git, but it can also be changed to other systems, currently supporting hg (mercurial), pijul (version control system written in Rust) and fossil.

Now look at the Cargo.toml file of the imgtool project, which defines the metadata and dependencies of the project, and is also the project manifest file.

image

This manifest file is still very streamlined at present, we will add some materials later. One thing needs to be mentioned, the toml file is the most ideal configuration file at present. It is simpler and more effective than YAML or JSON, and interested readers can search by themselves.

 

Cargo and dependencies

 

This section talks about how Cargo manages the direct or indirect dependencies between the project and other libraries, and how to ensure the repeated construction of the project.

 

The Rust project managed by Cargo has two files, which can currently meet all needs:

  • Cargo.toml, as a developer, you can use SemVer syntax (such as 1.3.*) in this file to write dependencies and their required versions,

  • Cargo.lock, which has been generated when the project is built, contains the absolute version of all direct dependencies and any indirect dependencies (such as 1.3.15).

    • This file ensures repeated builds of the binary crates.

    • Cargo uses this file to implement any further changes to the project to minimize the work required

    • Therefore, it is recommended that binary crates include .lock files in its repository, while library crates can be stateless and do not need to be included.

 

To update all dependencies, you can use cargo update. For updating individual dependencies, cargo update -p can be used. Moreover, if you update a separate crate version, Cargo will ensure that only the part of the corresponding library in the Cargo.lock file is updated, and other versions remain unchanged.

 

Cargo follows the semantic version control system (SemVer), that is, the library version number is specified in the format of major.minor.patch, and the rules are described as follows:

  • Major: Only increase when new major changes (including bug fixes) are made to the project

  • Minor: Only added when new features are added in a backward compatible way

  • Patch: Only added when the bug is fixed in a backward compatible way and no features are added

 

The above rules come from the summary of actual work experience, which has strong directivity and practical value. Next, let's take a look at the cargo build command, which is used to compile, link, and build our project.

 

This command does the following for the project:

  • If the Cargo.lock file has not been added, run cargo update, and add the exact version information from Cargo.toml

  • Download all dependencies resolved in Cargo.lock

  • Build all these dependencies

  • Build the project and link it with dependencies

 

By default, cargo build creates the debugging structure of the project in the target/debug/ directory, and passing a --release flag can easily create an optimized structure for the production code in the target/release/ directory. The Debug structure provides faster build time and shortens the feedback loop, while the production structure is slightly slower because the compiler runs more optimizations on the source code.

In the development process, it is often necessary to have a shorter repair-compile-check feedback time. For this reason, you can use the cargo check command to achieve a shorter compilation time, which basically skips the code generation part of the compiler. Only run the source code in the front-end stage, that is, perform parsing and semantic analysis in the compiler.

 

Another command is cargo run, which performs a dual task: run cargo build by hand, and then run the program in the target/debug/ directory. In order to build/run the release version, you can use cargo run-release.

Ok, so much has been said, although nothing has been added yet, run the original library with cargo run in the imgtool/ directory.

image

 

Conclusion

 

This article is still the main explanation. In the next article, we will first talk about how to use Cargo for code testing and show some new examples.

The imgtool library created in this article will appear here in subsequent practical projects.

 

 

Main references and documents recommended for readers to read further

https://doc.rust-lang.org/book

1. The Way of Rust Programming, 2019, Zhang Handong

2.The Complete Rust Programming Reference Guide,2019, Rahul Sharma,Vesa Kaihlavirta,Claus Matzinger

3.Hands-On Data Structures and Algorithms with Rust,2018,Claus Matzinger

4.Beginning Rust , 2018 , Carlo Milanesi

5.Rust Cookbook , 2017 , Vigneshwer Dhinakaran

Guess you like

Origin blog.csdn.net/qq_40433634/article/details/113075304