当前位置:网站首页>Manage rust project through cargo
Manage rust project through cargo
2022-06-27 19:56:00 【User 3147702】
1. introduction
After the introduction of the previous articles , We have made it clear that rust Basic grammar of :
Rust Basic grammar ( One ) -- Variable 、 Operation and annotation
Rust Basic grammar ( Two ) -- Functions and loops
As the programs we develop become more and more complex , When the cooperation in the project is more and more frequent , We must consider how to organize our project .
rust The official provided Cargo Tools to manage projects , Make our project management simple , In this article, we will introduce .
2. Use cargo Create project
2.1 Create project
stay rust In the toolkit , Has been installed by default cargo, You can verify by following the command cargo Whether it has been installed normally , And see its version :
$ cargo --version
Use cargo It's easy to create projects , Just execute the command with the project name as a parameter :
$ cargo new hello_cargo
This creates a file named hello_cargo Project .
Get into hello_cargo Directory execution tree The command can be seen :
.
├── Cargo.toml
└── src
└── main.rs
1 directory, 2 files
2.2 Cargo.toml
From the newly created hello_cargo From the contents of the project , In addition to what we are familiar with hello world programmatic main.rs Outside , The rest is just one Cargo.toml The file .
Cargo.toml The file is a TOML Standard format file , Its initial content is as follows :
[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
The third-party packages we need to rely on and their versions can be defined in this configuration file , stay build Automatically install these dependencies when .
stay rust in , Package is a pair of terms crate Translation , It is Rust The smallest compilation unit ,package It's a number of crate Set , In Chinese ,crate and package They are all called “ package ”, Sometimes we need to distinguish between packages , But in most cases it is not necessary to .
2.3 Code version control
Cargo By default git As a version control tool , Therefore, there are already... In the above project directory .gitignore file , Of course , When creating a project , It can also be done through --vcs Parameter to choose not to use any version control tools , Or by --help View other supported version control tool parameters .
3. use Cargo Build and run projects
Under the project directory , perform cargo build
Command can complete the construction of the task :
$ cargo build
Compiling hello_cargo v0.1.0 (/Users/techlog/Workspace/code/rust/hello_cargo)
Finished dev [unoptimized + debuginfo] target(s) in 2.01s
adopt tree command , You can see the new file generated after the build is completed :
.
├── Cargo.lock
├── Cargo.toml
├── src
│ └── main.rs
└── target
├── CACHEDIR.TAG
└── debug
├── build
├── deps
│ ├── hello_cargo-961ecc8ba604608e
│ ├── hello_cargo-961ecc8ba604608e.1qau2g7rc04b8n42.rcgu.o
│ ├── hello_cargo-961ecc8ba604608e.1wb2m83aihx8d8k4.rcgu.o
│ ├── hello_cargo-961ecc8ba604608e.2cxjpttsw6fohl8z.rcgu.o
│ ├── hello_cargo-961ecc8ba604608e.2ff6ogfujp34yrx2.rcgu.o
│ ├── hello_cargo-961ecc8ba604608e.48wqmd6nlep2ungd.rcgu.o
│ ├── hello_cargo-961ecc8ba604608e.54gbqr1pabl9mrem.rcgu.o
│ ├── hello_cargo-961ecc8ba604608e.5edo85wf9vpk0c0.rcgu.o
│ ├── hello_cargo-961ecc8ba604608e.cfznvsvgjwaw8ji.rcgu.o
│ └── hello_cargo-961ecc8ba604608e.d
├── examples
├── hello_cargo
├── hello_cargo.d
└── incremental
└── hello_cargo-cj2sb71suuq
├── s-gakbxnyar6-xajrwz-fg4gunogpa2i
│ ├── 1qau2g7rc04b8n42.o
│ ├── 1wb2m83aihx8d8k4.o
│ ├── 2cxjpttsw6fohl8z.o
│ ├── 2ff6ogfujp34yrx2.o
│ ├── 48wqmd6nlep2ungd.o
│ ├── 54gbqr1pabl9mrem.o
│ ├── 5edo85wf9vpk0c0.o
│ ├── cfznvsvgjwaw8ji.o
│ ├── dep-graph.bin
│ ├── query-cache.bin
│ └── work-products.bin
└── s-gakbxnyar6-xajrwz.lock
What we care about is actually target/debug/hello_cargo This executable file , It can be executed directly :
$ ./target/debug/hello_cargo
Hello, world!
In addition to these intermediate files , And the last executable file , In the project directory, there are also Cargo.lock file , It records the library and version number on which the current project is built .
If you want to compile the official release version , Then add... During construction --release
Parameters can be .
4. testing rust grammar
When you make some changes , You may not want to spend a long time compiling , Just want to know if there are compilation errors , here , Just execute cargo check
You can quickly locate compilation errors :
$ cargo check
Compiling hello_cargo v0.1.0 (file:///projects/hello_cargo)
Finished dev [unoptimized + debuginfo] target(s) in
0.32 secs
5. Run the project
perform cargo run
Command can run the project , If it hasn't been done before cargo build
Or in build Then there were new changes ,cargo run
The command will be executed automatically build:
$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.06s
Running `target/debug/hello_cargo`
Hello, world!
边栏推荐
- 刷题记录:Easy 数组(持续更新)
- 运算符的基础知识
- shell脚本常用命令(四)
- One week technical update express of substrate and Boca 20220425 - 20220501
- 華大單片機KEIL報錯_WEAK的解决方案
- MASS幸运哈希游戏系统开发丨冲突解决方法(代码分析)
- 基于STM32F103ZET6库函数按键输入实验
- 429- binary tree (108. convert the ordered array into a binary search tree, 538. convert the binary search tree into an accumulation tree, 106. construct a binary tree from the middle order and post o
- Embracing cloud Nativity: Practice of Jiangsu Mobile order center
- Is it safe to buy stocks online and open an account?
猜你喜欢
Erreur Keil de Huada Single Chip Computer La solution de Weak
Bit. Store: long bear market, stable stacking products may become the main theme
Blink SQL built in functions
数仓的字符截取三胞胎:substrb、substr、substring
【bug】联想小新出现问题,你的PIN不可用。
键入网址到网页显示,期间发生了什么?
Mathematical derivation from perceptron to feedforward neural network
Function key input experiment based on stm32f103zet6 Library
数智化进入“深水区”,数据治理是关键
《第五项修炼》(The Fifth Discipline):学习型组织的艺术与实践
随机推荐
键入网址到网页显示,期间发生了什么?
【debug】平台工程接口调试
1027 Colors in Mars
rust 中的结构体
Redis cluster Series III
作用域-Number和String的常用Api(方法)
【bug】联想小新出现问题,你的PIN不可用。
Erreur Keil de Huada Single Chip Computer La solution de Weak
labelimg使用指南
基于STM32F103ZET6库函数按键输入实验
蓄力中台,用友iuap筑牢社会级企业数智化新底座
1025 PAT Ranking
Photoshop layer related concepts layercomp layers move rotate duplicate layer compound layer
金鱼哥RHCA回忆录:DO447管理项目和开展作业--创建作业模板并启动作业
Buzzer experiment based on stm32f103zet6 library function
C# 二维码生成、识别,去除白边、任意颜色
Leetcode 1381. 设计一个支持增量操作的栈
刷题记录:Easy 数组(持续更新)
What is ssr/ssg/isr? How do I host them on AWS?
Pyhton爬取百度文库文字写入word文档