当前位置:网站首页>Rust Getting Started Guide (rustup, cargo)
Rust Getting Started Guide (rustup, cargo)
2022-07-28 19:28:00 【Wang Tai】
Rust Getting started (rustup, cargo)
Why use Rust ?
Rust It's a kind of System level programming language ,rust-lang.org There is a great definition :
“A language that enables everyone to build reliable and efficient software .
I like Rust, It is not only powerful but also very safe . Its ownership model can eliminate many common mistakes . But for C#、Java and JavaScript And other language programmers , The learning curve may be a little steep .
The purpose of this article is to Rust And its tool chain . If you want to know more about it, you can see the following
Rust Website Learn part (https://www.rust-lang.org/learn) Herbert Wolverson Of Hands on Rust (https://hands-on-rust.com/)
Let's get started Rust Well !
First, you need a development environment , You can use the local environment , But we recommend https://GeekCode.cloud Cloud development environment , It has Rust Dependency library and compilation acceleration function , It can make you more convenient to use rust function . open GeekCode After that, we choose Rust Environmental Science , You can operate the following commands .
install Rust
There are several ways to install on your system Rust( You can even be in Try it without installing (https://geekcode.cloud)). There are two key components to understand :
rustup Is a command line tool , It can be used as a tool chain of compilers . cargo It's for management projects、crates(rust package ) The command line , And we are dealing with Rust The most commonly used tool for projects .
We can :
from rust-lang.org Of Getting Started Partial access Rust, Or visit rustup.rs And follow the installation instructions , The easiest way is to visit GeekCode.cloud Use the cloud environment .
We installed rustup Or directly use the cloud environment , Try the following command :
Test your installation $ rustup -v
Check for updates $ rustup check
Update your installation $ rustup update
Now our Rust It's already running , Let's get into cargo.
Cargo — Rust Build tools and package managers
Cargo cli Tools are the most commonly used tools . It is responsible for running the build 、 Run tests and prepare projects for release . We still use cargo pack Rust( stay Rust The middle package is called crates), We can do it in crates.io View all crate.
First , Let's create a project , Then learn about some common cargo Options .
Create a new project
$ cargo new {project_name}
Create a new Hello World Program , It uses the default project structure 、* main.rs* Document and Cargo.toml Project documents . By default , This is a binary application ( If we need to develop library / crate, add to -lib
Options ).By default ,Git Initialized ( Other switches can be used to customize / Ban ) best Insist on using _snake_case Named after the , Because this is Rust standard .
Cargo Basic knowledge of
$ cargo check
Verify program structure —— This will not compile our program , But it will ensure that the project file structure is correct
$ cargo build
stay debugging Build applications in mode If it doesn't exist , This will create a target Folder add to -release Switch to release Pattern building ( See below )
$ cargo clean
clear ( Delete immediately ) Build file , Such as any binaries generated during construction
$ cargo run
Run the program - If not already built , This will also build the application
$ cargo test
Run any tests that exist in the project ( By default, there will be no tests in the scaffold )
Cargo target — Release vs Debug
By default ,cargo build
and cargo run
Will be in DEBUG Build the project in mode . To build a project to publish to the production environment , Please put --release
Parameters added to cargo —
$ cargo [build|run] --release
When you build a Rust Project time , Whatever the outcome , Will create a target Folder , And provide relevant build configurations below it . stay DEBUG When building in , The folder structure will be as follows -
…/project_root/target/debug/{build artefacts}
Rust It's a compiler language , This means that by default , The build will be done using the running machine architecture compilation . Rust Cross compiling is also supported , Allows us to build binaries to perform on other architectures .
for example ,Rust Compiled into WebAssembly (https://www.rust-lang.org/what/wasm)!, We can be like this in cargo build Add a target to the command ——
$ cargo build --release --target=wasm32-unknown-unknown
This will create the following folder structure ( Need to add --release
Parameters , And generated subfolders )-
…/project_root/target/wasm32-unknown-unknown/release/{build artefacts}
Code formatting
Rust There is a built-in formatter , You can Read here (https://github.com/rust-lang/rustfmt). After installation , We can run -
$ cargo fmt
This formats the code as Rust coding standard . If we want to customize the style , Can pass add to rustfmt.toml In your project .
Clippy — Code tips and suggestions
Rust There is a built-in tool for code tips and guidance ( namely linter), be called clippy, The details are in Clippy Of GitHub page (https://github.com/rust-lang/rust-clippy) . function clippy Will analyze the code and provide us with suggestions and improvements :
$ cargo clippy
We can do it in many ways To configure Clippy, But for beginners clippy Strict advice is very helpful . Just in our main.rs Add :
#![warn(clippy::all,clippy::pedantic)]
Clippy There are many levels of advice , But by default pedantic Level review , Usually enough .
summary
We should now be able to create a new Rust Project and to Rust Tool chain 、linter And suggestions have a good understanding .
Rust Although the standard library is great , But we still need to use Rust Many third-party libraries contributed by the community .
So next , We will continue to introduce Rust Package management .
Stay Rusty!
Join in GeekCode Community Participation defines Cloud Base IDE

Click on 【 Read the original 】 visit GeekCode
边栏推荐
猜你喜欢
Self-adaptive multi-objective evolutionary algorithm for flexible job shop scheduling with fuzzy pro
Cvpr21 unsupervised anomaly detection cutpaste:self supervised learning for anomaly detection and localization
Get to know nodejs for the first time (with cases)
ES6 conversion of new data type set and arr set map
Application of time series database in monitoring operation and maintenance platform
Dockler的基础用法
英文翻译意大利语-批量英文翻译意大利语工具免费
可转债概念表 x Notion 给你方便快捷的体验!
Photoshop responsive web design tutorial
Gmoea code operation 2 -- establishment and operation of operation environment
随机推荐
How to use Qianqian listening sound effect plug-in (fierce Classic)
Method of win7 system anti ARP attack
Application of time series database in cigarette factory
JS preventDefault() 键盘输入限制 onmousewheel stopPropagation停止事件传播
Srs4.0 installation steps
Streamlit machine learning application development tutorial
顺序线性表——课上练
Module 8 of the construction camp
ardupilot软件在环仿真与在线调试
SaltStack入门
R language text mining and natural language processing tutorial
ES6 conversion of new data type set and arr set map
英文翻译葡萄牙语-批量英文转换葡萄牙语-各种语言免费互译转换
DevCon. Exe export output to the specified file
Update of objects in ES6
Adobe XD web design tutorial
Pointer learning of C language -- the consolidation of pointer knowledge and the relationship with functions, arrays and structures
SaltStack之数据系统
Design of library management database system
图书管理数据库系统设计