当前位置:网站首页>[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 .
边栏推荐
- Foundation 31: Selenium positioning dynamic ID element
- Oracle10g单实例数据库升级到哪个版本好,求建议
- The annualization of financial products is 4%. How much profit can you get from buying 10000 yuan a month?
- How to avoid duplicate data when the database is high and distributed
- Solve NPM error: cannot find module 'shelljs‘
- JMeter test plan cannot be saved solution
- 【npm】 无法将“npm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
- js小游戏源码魔塔闯关下载
- LabVIEW experiment - temperature detection system (experimental learning version)
- Graduation project of wechat small program ordering system of small program completion works (7) Interim inspection report
猜你喜欢

Oracle10g单实例数据库升级到哪个版本好,求建议

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

这家十年内容产业基建公司,竟是隐形的Web3先行者

The operation cannot be completed because a folder or file in it is already open in another program

Foundation 31: Selenium positioning dynamic ID element

这是我见过写得最烂的Controller层代码...

(self drawn ugly picture) simple understanding tcp/ip three handshakes and four waves

Wechat reservation of the completed works of the applet graduation project (6) opening defense ppt

Source code of short video live broadcast system

Intel apologized to the winners of Xe HPG treasure hunt game for product delay and announced the appearance of the prize
随机推荐
全网最简约的sklearn环境配置教程(百分百成功)
CIR industrial automation radar
JS small game source code magic tower breakthrough Download
Django4.0 + Web + MySQL5.7 实现简单登录操作
Labview--- signal generator
51 single chip microcomputer key control LED light status
51 MCU peripherals: buzzer
Asp. Net core CMD common instructions
图解LeetCode——1184. 公交站间的距离(难度:简单)
Canvas dynamic picture avatar shaking JS special effect
Unity ugui interaction (new ideas)
LeetCode·83双周赛·6129.全0子数组的数目·数学
音乐人的 NFT 指南
51单片机内部外设:串口通信
Phpexcel reports an error: err_ INVALID_ RESPONSE
Sticky.js page scrolling div fixed position plug-in
Redis learning notes
How does Flink SQL persist?
JDBC快速入门
JS pop-up City filtering component matches mobile terminal