当前位置:网站首页>[learn rust together] a preliminary understanding of rust package management tool cargo
[learn rust together] a preliminary understanding of rust package management tool cargo
2022-07-25 09:04:00 【Guanglongyu】
Tips : It is suggested to study this chapter after learning the previous section , Please subscribe to this column for details .
List of articles
Preface
In the last article we learned Rust Of Hello World, And it is analyzed in detail , I believe you have learned , In this section we will use Cargo Create a Hello World, And analyze it in detail .
We wrote last time Hello World Is the simplest program , There is no dependence . Think , If you are developing a large project , It will definitely reference other libraries , Or manage some files , Build , At this time, compilation becomes a complex problem .Rust The authorities provided it to us Cargo To manage these projects , It can do everything mentioned above , Use Cargo Managing will make your development easier , All we use in our future development are Cargo 了 .
Cargo yes Rust Build system and package manager . majority Rustacean We use it Cargo To manage their Rust project , Because it can handle many tasks for you , Like building code 、 Download dependent libraries and compile them .
RustaceanIt refers to the big guy who studies in front of the computer .
One 、 Use Cargo Create project
First, you need to check whether your computer is installed Cargo, Of course , If you install it normally, it must have been installed .
cargo --version
Go back to , It means there is no problem with the installation , Otherwise, it is recommended that you reinstall Rust.
1. New projects
The following command is used to create a new project
cargo new hello_cargo
among hello_cargo Is the name of the new project , After running, the following figure indicates that the creation is successful .
Next, enter the project directory just created , You can see the directory structure as follows ,
│ .gitignore
│ Cargo.toml
│
└─src
main.rs
- .gitignore It's a git Ignore file list , Upload to git when , Not all files should be uploaded to github Of . It also reminds us ,cargo For us to create a github Warehouse .
- Cargo.toml It is the configuration file of a project , This includes the project name , Dependency and other contents , Detailed introduction later .
- src/main.rs It comes with the creation of the project Hello World file .
2. Specify the version management system
cargo Created by default for us git Warehouse , because git Is a commonly used management system , in addition to ,cargo You can also specify a version management system , Use --vcs Specify parameters or switch the version management system , If the folder already has a warehouse , Then the warehouse will not be created by default .
cargo new --vcs=git
--vcs The options are
| optional |
|---|
| git, hg, pijul, fossil, none |
3. Cargo.toml The configuration file
Cargo.toml The file USES TOML Configuration format , stay TOML In the format
[section]Called a segment Title- name = value Configuration items under the form configuration Title
The configuration file for the project , You can open... With a text editor , The contents are as follows
[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"
[dependencies]
[package]It is used to manage the information of the current project package
–nameIndicates that the project name is hello_cargo
–versionIndicates that the project version number is 0.1.0
–editionIndicates the Rust Version number[dependencies]It is used to manage the dependency information of the current project package . stay Rust in , The code package is calledcrates. This project doesn't need anything elsecrate, However, dependency will be used in later projects , This clip will be used at that time .
Two 、 Build and run Cargo project
Use cargo Running the program is different from running the program manually , To run the program manually, you need to compile , And then it runs again , stay cargo We can complete this operation in one step ,cargo This process is automatically managed for us .
1. Build the project
Build the project using the following commands ,
cargo build
After operation , The compilation is successful, as shown in the figure below ,
here cargo Just compile the project for us , find hello_cargo\target\debug Directory, you can see our compiled executable , This is the same as the file we execute on the command line

The executable program runs the same as before 
Cargo.lock file
If the project compiles normally , Will generate a Cargo.lock file , This file records the actual version the project depends on . Just you yourself? forever You don't need to touch this file , Give Way Cargo Just deal with it .
2. Run the project
Use cargo To run the project , Use the following command
cargo run
The operation effect is the same as that of our manual operation ,run The compilation and running steps will be automatically executed , We have compiled above , Therefore, the runtime does not need to compile again ,cargo Become very intelligent .
After being modified again, there will be a compilation process 
3. Check code
Cargo It also provides a tool for code checking , This command quickly checks the code to ensure that it can be compiled , But it does not produce executable files :
cargo check
When we do it , There is no wrong report , There is no problem with the code .
3、 ... and 、 Release Cargo project
When publishing a project , We use the following commands to optimize the compilation project , In order to let Rust Code runs faster , However, enabling these optimizations also takes longer compilation time .
cargo build --release
- Debug It's for development , You need to rebuild frequently and quickly ;
- Release Is to build the final program for users , They don't often rebuild , And I hope the program can run as fast as possible .

Finally, you can target/release Find the final executable file under .
summary
Let's review what we have learned Cargo Content :
- have access to cargo new Create project .
- have access to cargo build Build the project .
- have access to cargo run Build and run the project in one step .
- have access to cargo check Build the project to check for errors without generating binaries .
- It is different from putting the build results in the same directory as the source code ,Cargo Will put it in target/debug Catalog .
边栏推荐
- How does Youxuan database encrypt data?
- 51单片机外设篇:电机
- How to avoid duplicate data when the database is high and distributed
- 附加:在在下部分区/县(数据表)
- Solve the syntaxerror: unexpected end of JSON input
- Wechat reservation applet graduation design of applet completion works (3) background function
- Wechat reservation of the completed works of the applet graduation project (6) opening defense ppt
- Read and write models and organize notes
- The international summit osdi included Taobao system papers for the first time, and end cloud collaborative intelligence was recommended by the keynote speech of the conference
- Sticky.js page scrolling div fixed position plug-in
猜你喜欢
![[stl]list Simulation Implementation](/img/92/2a78382700c1ebf299c6505d962c9c.png)
[stl]list Simulation Implementation

PL/SQL工具导出sql文件所使用的命令是什么?

51单片机外设篇:电机

LeetCode·83双周赛·6129.全0子数组的数目·数学

整理 华为AP-3010DN_V2配置创建wifi

Graduation project of wechat small program ordering system of small program completion works (1) development outline

JD cloud and Forrester consulting released a hybrid cloud report that cloud Nativity has become a new engine driving industrial development

PHP reports an error: classes\phpexcel\cell php Line(594) Invalid cell coordinate ESIGN1

Wechat reservation applet graduation project (7) mid term inspection report of applet completion works

Wechat sports ground reservation applet graduation project of applet completion works (1) development outline
随机推荐
2022-7-14 JMeter pressure test
table表格展开内部行切换效果
The annualization of financial products is 4%. How much profit can you get from buying 10000 yuan a month?
51 MCU internal peripherals: serial port communication
Solving a random number problem
Labview--- signal generator
全网最简约的sklearn环境配置教程(百分百成功)
Wechat sports field reservation of the finished works of the applet graduation project (4) opening report
附加:下半部分sql语句 区/县(数据表)
How to realize the drop-down option box of wechat applet
[stl]list Simulation Implementation
智能运维场景解析:如何通过异常检测发现业务系统状态异常
NFT guide for musicians
Tips for improving code sustainability, take connectto method as an example.
Wechat reservation of the completed works of the applet graduation project (6) opening defense ppt
How to use pixi.js to make simple Parkour games
Disable module (attribute node) in LabVIEW
Wechat reservation of completed works of applet graduation project (4) opening report
A page widgetization practice
Wechat reservation applet graduation design of applet completion works (3) background function