当前位置:网站首页>[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 .
边栏推荐
- [STL]stack&queue模拟实现
- The hole of scroll view in uniapp
- Wechat applet ordering system graduation design of applet completion works (8) graduation design thesis template
- When crontab scheduled task executes jar through script, it encounters a pit where jar package execution is invalid
- 25 Ph.D. degrees revoked
- Wechat reservation of completed works of applet graduation project (4) opening report
- The development of art NFT
- Redis learning notes
- 图解LeetCode——919. 完全二叉树插入器(难度:中等)
- Overview of redis/mysql knowledge
猜你喜欢
![[graduation project] cinema booking management system based on micro Service Framework](/img/74/b12f1fe105c117aa96ebaa4226f85b.png)
[graduation project] cinema booking management system based on micro Service Framework
![[STL]list模拟实现](/img/92/2a78382700c1ebf299c6505d962c9c.png)
[STL]list模拟实现

CIR industrial automation radar

What is steel grating?

How to do the game plug-in?

Redis学习笔记

Source code of short video live broadcast system

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

What are the commands used by pl/sql tools to export SQL files?

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
随机推荐
Graduation design of wechat small program ordering system of small program completion works (3) background function
Solve the syntaxerror: unexpected end of JSON input
The operation cannot be completed because a folder or file in it is already open in another program
Wechat sports ground reservation applet graduation design of applet completion works (2) applet function
Robot jumping problem
JS small game source code magic tower breakthrough Download
Additional: SQL statement area / county in the lower half (data table)
51 single chip microcomputer controls nixie tube display
Phpexcel reports an error: err_ INVALID_ RESPONSE
When testing VPN, the IP found by the command line is inconsistent with that of Baidu search
游戏外挂怎么做?
JD cloud and Forrester consulting released a hybrid cloud report that cloud Nativity has become a new engine driving industrial development
What version of Oracle10g single instance database is better to upgrade to? Ask for suggestions
Wechat Reservation Reservation of applet completion works applet graduation project (8) graduation project thesis template
附加:中半部分sql语句 区/县(数据表)
艺术 NFT 的发展之路
[untitled]
The development of art NFT
Solving a random number problem
防抖与节流