开发语言 后端 2027年3月22日:使用Rust迈出第一步--设置Rust开发环境? - cargo run

📅 2026-11-30 👤 拼多多助力营销技巧 📂 拼多多助力网站 👁️ 582 次阅读

简介

若要使用 Rust 进行编程,需安装 Visual Studio Code 编辑器、适用于 Visual Studio Code 的 Microsoft C++ 生成工具和 Rust 语言文件。

安装Visual Studio Code

Rust源文件是文件扩展名为.rs的文本文件。在文本文件中保存代码后,使用Rust编译器(rustc)或Cargo将代码编译成程序。

用于编写Rust代码的工具

在Windows上安装 Visual Studio Code

步骤1-下载安装程序

步骤2-启动安装程序

步骤3-启动Visual Studio Code

安装Visual C++生成工具

Rust需要Visual Studio的Microsoft C++生成工具。必须先安装这些编译工具,然后才能安装Rust。

1、登陆Microsoft Visual Studio下载页面

2、选择“下载生成工具”。

3、下载完成后,运行安装程序文件。

4、在弹出窗口中,选择“是”。在下一个弹出对话框中,选择“继续”。

5、在安装程序窗口中的“桌面应用和移动应用”下,选中左侧“C++生成工具”选项的复选框。 拼多多帮助力入口

6、在左侧的“安装详细信息”窗格中,确保选中以下选项:

7、在右下角,选择“安装”。

安装Rust

请到网站rustup.rs查找操作系统 的相应说明。

Rust有一个为期六周的快速发布过程,并且大量的平台。

PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Rust>rustup update

info: syncing channel updates for 'stable-x86_64-pc-windows-msvc'

696.7 KiB / 696.7 KiB (100 %) 202.5 KiB/s in 4s ETA: 0s

info: latest update on 2022-04-07, rust version 1.60.0 (7737e0b5c 2022-04-04)

info: downloading component 'cargo'

1.9 MiB / 3.9 MiB ( 48 %) 153.6 KiB/s in 45s ETA: 13s

info: retrying download for 'https://static.rust-lang.org/dist/2022-04-07/cargo-1.60.0-x86_64-pc-windows-msvc.tar.xz'

info: retrying download for 'https://static.rust-lang.org/dist/2022-04-07/cargo-1.60.0-x86_64-pc-windows-msvc.tar.xz'

info: retrying download for 'https://static.rust-lang.org/dist/2022-04-07/cargo-1.60.0-x86_64-pc-windows-msvc.tar.xz'

info: retrying download for 'https://static.rust-lang.org/dist/2022-04-07/cargo-1.60.0-x86_64-pc-windows-msvc.tar.xz'

error: component download failed for cargo-x86_64-pc-windows-msvc

info: checking for self-updates

error: could not download file from 'https://static.rust-lang.org/rustup/release-stable.toml' to 'C:\Users\A-XIAO~1\AppData\Local\Temp\rustup-updateTOdYIS\release-stable.toml': failed to make network request: error sending request for url (https://static.rust-lang.org/rustup/release-stable.toml): operation timed out: operation timed out

//clash

PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Rust>rustup update

info: syncing channel updates for 'stable-x86_64-pc-windows-msvc'

error: could not download file from 'https://static.rust-lang.org/dist/channel-rust-stable.toml.sha256' to 'C:\Users\a-xiaobodou\.rustup\tmp\5lq33lvozce3ydbb_file'

info: checking for self-updates

error: could not download file from 'https://static.rust-lang.org/rustup/release-stable.toml' to 'C:\Users\A-XIAO~1\AppData\Local\Temp\rustup-updatec4nqaO\release-stable.toml': failed to make network request: error sending request for url (https://static.rust-lang.org/rustup/release-stable.toml): operation timed out: operation timed out

//Shanghai

PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Rust>rustup update

info: syncing channel updates for 'stable-x86_64-pc-windows-msvc'

696.7 KiB / 696.7 KiB (100 %) 64.0 KiB/s in 11s ETA: 0s

info: latest update on 2022-04-07, rust version 1.60.0 (7737e0b5c 2022-04-04)

info: downloading component 'cargo'

3.2 MiB / 3.9 MiB ( 82 %) 54.4 KiB/s in 2m 22s ETA: 12s

info: retrying download for 'https://static.rust-lang.org/dist/2022-04-07/cargo-1.60.0-x86_64-pc-windows-msvc.tar.xz'

3.9 MiB / 3.9 MiB (100 %) 73.6 KiB/s in 1m 53s ETA: 0s

info: downloading component 'clippy'

1.7 MiB / 1.7 MiB (100 %) 195.4 KiB/s in 45s ETA: 0s

info: downloading component 'rust-docs'

10.5 MiB / 19.6 MiB ( 54 %) 46.5 KiB/s in 9m 22s ETA: 3m 18s

info: retrying download for 'https://static.rust-lang.org/dist/2022-04-07/rust-docs-1.60.0-x86_64-pc-windows-msvc.tar.xz'

286.9 KiB / 19.6 MiB ( 1 %) 23.6 KiB/s in 1m 59s ETA: 13m 55s

info: retrying download for 'https://static.rust-lang.org/dist/2022-04-07/rust-docs-1.60.0-x86_64-pc-windows-msvc.tar.xz'

2.8 MiB / 19.6 MiB ( 14 %) 67.2 KiB/s in 2m 33s ETA: 4m 15s

info: retrying download for 'https://static.rust-lang.org/dist/2022-04-07/rust-docs-1.60.0-x86_64-pc-windows-msvc.tar.xz'

info: retrying download for 'https://static.rust-lang.org/dist/2022-04-07/rust-docs-1.60.0-x86_64-pc-windows-msvc.tar.xz'

error: component download failed for rust-docs-x86_64-pc-windows-msvc

info: checking for self-updates

stable-x86_64-pc-windows-msvc update failed - rustc 1.59.0 (9d1b2106e 2022-02-23)

info: cleaning up downloads & tmp directories

//Beijing

PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Rust>

检查Rust安装

PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Rust>rustc --version

rustc 1.59.0 (9d1b2106e 2022-02-23)

PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Rust>cargo --version

cargo 1.59.0 (49d8809dc 2022-02-10)

PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Rust>

这两个输出行都包含有发行版号,提交哈希,提交日期

该信息显示为以下格式:

(<9-character-hash-code><4-digit-year>-<2-digit-month>-<2-digit-day>)

Hello World

创建一个新目录来组织代码

首先创建一个目录来存储此学习路径中的所有代码(rust-learning-path),然后创建一个新的子目录来保存此练习的源代码。

PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Rust>mkdir rust-learning-path

Directory: C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Rust

Mode LastWriteTime Length Name

---- ------------- ------ ----

d----- 2022/5/14 17:40 rust-learning-path

PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Rust>cd /d rust-learning-path

Set-Location : A positional parameter cannot be found that accepts argument 'rust-learning-path'.

At line:1 char:1

+ cd /d rust-learning-path

+ ~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : InvalidArgument: (:) [Set-Location], ParameterBindingException

+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.SetLocationCommand

PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Rust>cd rust-learning-path

PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Rust\rust-learning-path>mkdir hello-world

Directory: C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Rust\rust-learning-path

Mode LastWriteTime Length Name

---- ------------- ------ ----

d----- 2022/5/14 17:41 hello-world

PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Rust\rust-learning-path>cd hello-world

PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Rust\rust-learning-path\hello-world>

PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Rust\rust-learning-path\hello-world>notepad main.rs

编写第一个Rust程序:

fn main(){

println!("Hello,World!");

}

编译并运行程序:

PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Rust\rust-learning-path\hello-world>rustc main.rs

PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Rust\rust-learning-path\hello-world>.\main.exe

Hello,World!

PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Rust\rust-learning-path\hello-world>

使用Cargo创建项目:

PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Rust\rust-learning-path>cargo new hello-cargo

Created binary (application) `hello-cargo` package

PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Rust\rust-learning-path>

此命令生成名为hello-cargo的新目录,其中包含src子目录,并添加两个文件。

Cargo.toml是Rust的清单文件。这个文件可用于保存项目及依赖项的元数据。

src子目录中的main.rs文件可用于编写应用程序代码。

使用Cargo生成并运行程序:

PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Rust\rust-learning-path>cd hello-cargo

PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Rust\rust-learning-path\hello-cargo>cargo run

Compiling hello-cargo v0.1.0 (C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Rust\rust-learning-path\hello-cargo)

Finished dev [unoptimized + debuginfo] target(s) in 1.68s

Running `target\debug\hello-cargo.exe`

Hello, world!

PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Rust\rust-learning-path\hello-cargo>

Cargo已生成并执行了可执行文件。

文章来源

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧