当前位置:网站首页>Rust Getting Started Guide (crite Management)
Rust Getting Started Guide (crite Management)
2022-07-28 19:28:00 【Wang Tai】
Rust Getting started (crate management )
This paper is about 「Rust Getting started 」 The second part of the series . This article will focus on cargo cli Tools and Rust Package management system , Include :
This paper mainly introduces cargo Command line and Rust Package management system , Include
Search for crates Used in projects crates To configure crate install / Use cargo-watch
Rust You can refer to the previous article for the basic knowledge of Rust — Getting started (rustup, cargo)
have access to https://geekcode.cloud One click creation of cloud development environment rust The environment quickly starts the following content .
The following content defaults that you have read the content of the previous article , And in https://geekcode.cloud Experienced rustc and rustup.
Crates
crates Namely Rust package . Generated crates Registered in crates.io On the warehouse , We can do it in crates.io Find all crates.
Use cargo The command to search crate. for example , To search and sql dependent crates :
$ cargo search sql
search The result of the command shows the relevant crate Name and version number :

Use Crates
Put what you need crates Add to Cargo.toml In file .
Use $ cargo new {project_name} Created Cargo.toml Content :
[package]
name = "hello_world"
version = "0.1.0"
edition = "2022"# See more keys and their definitions athttps://doc.rust-lang.org/cargo/reference/manifest.html[dependencies]
edit Cargo.toml file , Add the search above sql create:
[package]
name = "hello_world"
version = "0.1.0"
edition = "2018"# See more keys and their definitions athttps://doc.rust-lang.org/cargo/reference/manifest.html[dependencies]
sql = "0.4.3" <-- Add dependency here
Be careful ,Cargo Dependencies and SemVer Syntax is fully compatible , Allow selective updates on demand .
Crates、Git and Feature toggles
Cargo There are powerful functions to obtain crates . You can specify in different ways crate .
Standard way :
[dependencies]
bracket-lib = “~0.8.1”
Appoint Git Warehouse :
[dependencies]
bracket-lib ={ git = "https://github.com/thebracket/bracket-lib" }
Use feature toggles , In the following example , Use bracket-lib , But disable the default rendering engine and use vulkan engine -
[dependencies]
bracket-lib = {
git = "https://github.com/thebracket/bracket-lib",
default-features = false,
features = [ "amethyst_engine_vulkan" ]
}
Source (Source) And binary Crates
C# or NPM The engineer , You will notice that you are adding crate Post run $ cargo build Console output when :
Compiling sql v0.4.3
Compiling hello_world v0.1.0 (/Users/gian/hello_world)
Finished dev [unoptimized + debuginfo] target(s) in 0.99s
majority Rust crate Are based on source code , When adding to a local project ,cargo Will automatically get the source code , Then compile on the platform crate.
If you specify a different target platform ,cargo Will recompile all crate.
Cargo Installation Support binary crates, Specific to crates It's an executable . therefore cargo obtain 、 Compile and then install the generated executable .
To install binary crate, You can use cargo:
$ cargo install {crate_name}
Be careful : Use cargo install Only binary crate. You cannot use this command to install / Add routine crate.
Please see the following cargo-watch An example of .
Cargo-watch
For most crate, You can add them directly to Cargo.toml. use first cargo search Command to find the latest version , Or go directly to crates.io, but You can't Use cargo install.
however , If installed crate yes Binary system crate, Then only use cargo install Can get 、 Compile and install crate For your use .
Be careful : And others crate Different , Install binary crate Only once ( Unless you need to update the binary crate Version of ).
Updating crates.io index
Ignored package `cargo-watch v7.8.1` is already installed, use --force to override
cargo-watch Used to monitor file changes in the project and run commands .
install cargo-watch :
$ cargo install cargo-watch
After installation , Can be accessed immediately cargo-watch Tools :
$ cargo-watch -x build
This command will start a process to monitor the changes of files in the project , under these circumstances , Automatically run when changes are detected cargo build.
You can link multiple commands together like this -
$ cargo-watch -x fmt -x test
If you have installed Rust linter, But it triggers linter, Then run any tests every time the file changes !
You can use cargo-watch Do more , So please check cargo-watch crate file .
summary
Rust crates It is not particularly different from most other packaging systems , But as mentioned above , There are few things to pay attention to .
cargo-watch crate It's binary crate Example , It is also a very convenient utility , When you are in VSCode When you are developing in and want to give immediate feedback on code changes .
There are many other useful crates stay crates.io On .
keep rusty!
Join in GeekCode Community Participation defines Cloud Based IDE

Click on 【 Read the original 】 visit GeekCode
边栏推荐
- BLDC 6-step commutation simulink
- Force buckle 1331. Array serial number conversion
- [solved] ac86u ml revision firmware virtual memory creation failed, prompting that the USB disk reading and writing speed does not meet the requirements
- Failed to install app-debug.apk: Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI]
- BeanFactory not initialized or already closed - call ‘refresh‘ before accessing beans via the Applic
- 英文翻译意大利语-批量英文翻译意大利语工具免费
- SaltStack进阶
- Server body 21: pre compilation processing by different compilers (a brief introduction to MSVC and GCC)
- Pandownload revival tutorial
- CVPR19 - 调参干货《Bag of Tricks for Image Classification with Convolutional Neural Network》
猜你喜欢

剑指 Offer II 109. 开密码锁

Streamlit machine learning application development tutorial

Pytoch: implementation of crossentropyloss and labelsmoothing

VAE:变分自编码器的理解与实现

Libgdx learning path 01: libgdx introduction and running tutorial

Kotlin Android development novice tutorial

SRS4.0安装步骤

CVPR19 - 调参干货《Bag of Tricks for Image Classification with Convolutional Neural Network》

Photoshop web design practical tutorial

Pytorch:快速求得NxN矩阵的主对角线(diagonal)元素与非对角线元素
随机推荐
使用SaltStack自动化部署Zabbix
以数字化转型为契机,3C企业如何通过SRM供应商云协同平台实现高效协同?
ACM warm-up exercise 3 in 2022 summer vacation (detailed)
It is the best tool to evaluate six kinds of map visualization software in three categories
When CNN meets transformer cmt:revolutionary neural networks meet vision transformers
JS 批量添加事件监听onclick this 事件委托 target currentTarget onmouseenter onmouseover
Time waits for no man. The rise of TSDB is at the right time
Powerbi time series analysis, prediction and visualization tutorial
英文翻译阿拉伯语-批量英文翻译阿拉伯语工具免费
图书管理数据库系统设计
6-20 vulnerability exploitation proftpd test
Application value of MES production management system to equipment
Using CPLEX to solve small-scale energy-efficient FJSP
智能合约安全——溢出漏洞
Self-adaptive multi-objective evolutionary algorithm for flexible job shop scheduling with fuzzy pro
Mid 2022 summary
Validate hardware DDR design with Xilinx MIG
企业级分布式爬虫框架入门
RFs self study notes (II): theoretical measurement model - without clutter but with detection probability
About ASM redundancy