当前位置:网站首页>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 --versionUse cargo It's easy to create projects , Just execute the command with the project name as a parameter :
$ cargo new hello_cargoThis 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 files2.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.01sadopt 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.lockWhat 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 secs5. 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!边栏推荐
猜你喜欢
随机推荐
SQL Server - Window Function - 解决连续N条记录过滤问题
Pyhton crawls Baidu library text and writes it into word document
【help】JVM的CPU资源占用过高问题的排查
mime.type文件内容
ABAP-SM30删除前检查
基于STM32F103ZET6库函数外部中断实验
Garbage collector driving everything -- G1
429-二叉树(108. 将有序数组转换为二叉搜索树、538. 把二叉搜索树转换为累加树、 106.从中序与后序遍历序列构造二叉树、235. 二叉搜索树的最近公共祖先)
券商经理的开户二维码开户买股票安全吗?有谁知道啊
Substrate及波卡一周技术更新速递 20220425 - 20220501
Tupu digital twin intelligent energy integrated management and control platform
Common shell script commands (4)
Common shell script commands (III)
MASS幸运哈希游戏系统开发丨冲突解决方法(代码分析)
Solution of adding st-link to Huada MCU Keil
One to one relationship
数仓的字符截取三胞胎:substrb、substr、substring
过关斩将,擒“指针”(下)
shell脚本常用命令(三)
GIS remote sensing R language learning see here








