Rust 从入门到摔门而出门 环境安装 Hello, World!

345 阅读3分钟

环境安装

在 Linux 或者 macOS 上安装 RustUp 环境

打开终端,执行下面命令

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

命令不对可以打开 rust官网 查看最新的 rustup 脚本连接

Windows 安装 打开链接: forge.rust-lang.org/infra/other… 在底部找到对应系统的安装包下载 ,官方 Rust 独立安装程序包含 Rust 的单个版本,适合离线安装。它们具有三种形式:.tar.gz可在任何类 Unix 环境中运行的 tarball(扩展名)、Windows 安装程序 ( .msi) 和 Mac 安装程序 ( .pkg)

安装成功 会 出现 Rust is installed now. Great!

或者使用下面命令查看是否安装成功:

rustc --version

输出 版本号 和 对应的 Commit Hash 和 Commit 日期

安装错误

  1. 缺少 C ++编译器
    在 macOS 上,你可以通过运行以下命令获得 C 语言编译器:
    xcode-select --install
    在 Windows 上安装 Visual Studio 下载链接:visualstudio.microsoft.com/zh-hans/dow…
    一定要勾选 C ++ 和 Windows 10(或 11)SDK

  2. 检查系统环境变量中是否有 Rust。

    Linux 或者 macOS

    echo $PATH

    Windows

    echo %PATH%

更新和卸载

更新命令

 rustup update

卸载Rust

 rustup self uninstall

Hello, World!

创建一个文件,命名 mian.rs。 Rust 语言使用.rs 扩展名结尾。书写下面代码:

fn main() {
    println!("Hello, world!");
}

保存文件,执行下列命令

 # 执行命令生成 mian 文件, Windows上会生成 mian.exe 文件
 rustc main.rs
 # 执行 mian 文件(Windows上执行./mian.exe ), 输出 Hello, world!
 ./mian

分析 Hello, World! 代码

fn main() {
    
}

上述代码 声明了 main函数,在rust中 fn 声明函数,main函数名,() 函数参数。和JavaScript声明函数类似。 但是 main函数在Rust中是一个特殊的函数,在可执行的Rust中,它总是最先运行的。

  println!("Hello, world!");

上述代码中是将 "Hello, world!" 字符串打印在控制台中。println! 是调用Rust的宏,println后面! 代表是Rust的宏, 函数名后有!代表调用宏,不是普通的函数,并且宏并不总是遵循与函数相同的规则。(后面会详细讲解 宏和函数的区别)

Rust的包管理器 Cargo

执行命令,检查是否安装 Cargo。

cargo --version

输出版本号 类似:cargo 1.74.1 (ecb9851af 2023-10-18) 说明安装成功,失败检查前面说的环境安装是否正确。

使用 cargo new 创建项目 hello_cargo

cargo new hello_cargo

输出: Created binary (application) 'hello_cargo' package 说明执行完成,项目创建成功,目录下多个 hello_cargo 文件夹,

.
├── Cargo.toml
├── .gitignore
└── src
    └── main.rs

.gitignore 文件不用多说,就是提交git的忽略文件。

Cargo.toml 文件

[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]

[package]是表明下面的是项目的配置包,name项目名,version项目版本,edition使用的Rust版本值,目前Rust有三个可用版本 Rust 2015、Rust 2018 和 Rust 2021。 [dependencies]是表示项目的依赖。

src/main.rs文件

fn main() {
    println!("Hello, world!");
}

文件中的代码在上面讲了,下面直接打包和运行。

运行下列命令,执行打包

cargo build
   # 输出: Compiling hello_cargo v0.1.0 (/Users/Desktop/hello_cargo)
   # 输出: Finished dev [unoptimized + debuginfo] target(s) in 5.28s

项目中生成target文件夹

└── target
    ├── CACHEDIR.TAG
    └── debug
        ├── build
        ├── deps
        │   ├── hello_cargo-a1f21a4d52da3e0b
        │   ├── hello_cargo-a1f21a4d52da3e0b.3231yef6husp4ddt.rcgu.o
        │   ├── hello_cargo-a1f21a4d52da3e0b.37sdwxqpuxtu2snh.rcgu.o
        │   ├── hello_cargo-a1f21a4d52da3e0b.4em3h7osa6tnf2p2.rcgu.o
        │   ├── hello_cargo-a1f21a4d52da3e0b.50l93nlojbrf81qi.rcgu.o
        │   ├── hello_cargo-a1f21a4d52da3e0b.56xem4cvf5ag3zeo.rcgu.o
        │   ├── hello_cargo-a1f21a4d52da3e0b.d
        │   └── hello_cargo-a1f21a4d52da3e0b.mpat6w57bkh6npw.rcgu.o
        ├── examples
        ├── hello_cargo
        ├── hello_cargo.d
        └── incremental
            └── hello_cargo-2i1cc5ixgm7c7
                ├── s-gsctrlm17h-ykoxtd-a7zicxfr117j2bgas6kr34p0i
                │   ├── 3231yef6husp4ddt.o
                │   ├── 37sdwxqpuxtu2snh.o
                │   ├── 4em3h7osa6tnf2p2.o
                │   ├── 50l93nlojbrf81qi.o
                │   ├── 56xem4cvf5ag3zeo.o
                │   ├── dep-graph.bin
                │   ├── mpat6w57bkh6npw.o
                │   ├── query-cache.bin
                │   └── work-products.bin
                └── s-gsctrlm17h-ykoxtd.lock

直接执行命令:

./target/debug/hello_cargo

或者使用

 cargo run

运行项目,在控制台中输出 Hello, world!

check 命令可以检查代码,保证其可以编译。

cargo check

发布

项目准备发布时,使用 --release 优化编译项目。

cargo build --release