Rust 初体验

近日,在安装某ss-rust时,发现要在Debian上可选安装方式有snapcargo

由于众所周知的snap稳定性,还是选择cargo进行后续操作。

不熟悉的领域总会出错,本次cargo的安装固然不是一帆风顺。

安装 Cargo

根据文档1,安装cargo似乎非常简单,只需要执行下面这一段指令确认结果就可以了。

1
$ curl https://sh.rustup.rs -sSf | sh

但实际上,安装结果确实符合官网所描述的 Rust is installed now. Great!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
info: downloading installer

Welcome to Rust!

This will download and install the official compiler for the Rust
programming language, and its package manager, Cargo.

Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:

/root/.rustup

This can be modified with the RUSTUP_HOME environment variable.

The Cargo home directory is located at:

/root/.cargo

This can be modified with the CARGO_HOME environment variable.

The cargo, rustc, rustup and other commands will be added to
Cargo's bin directory, located at:

/root/.cargo/bin

This path will then be added to your PATH environment variable by
modifying the profile files located at:

/root/.profile
/root/.bashrc

You can uninstall at any time with rustup self uninstall and
these changes will be reverted.

Current installation options:


default host triple: x86_64-unknown-linux-gnu
default toolchain: stable (default)
profile: default
modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation

>

info: profile set to 'default'
info: default host triple is x86_64-unknown-linux-gnu
warning: Updating existing toolchain, profile choice will be ignored
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: default toolchain set to 'stable-x86_64-unknown-linux-gnu'

stable-x86_64-unknown-linux-gnu unchanged - rustc 1.71.1 (eb26296b5 2023-08-03)


Rust is installed now. Great!

To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo's bin directory ($HOME/.cargo/bin).

To configure your current shell, run:
source "$HOME/.cargo/env"

问题在哪

安装 Cargo 后,我们根据文档执行cargo new hello_world,似乎没什么问题。

但是执行构建指令后,问题显然就发生了。这是由于没有安装编译器。(在纯净环境下总是会忘记装编译环境)

1
2
3
4
5
6
7
$ cargo build
Compiling hello_world v0.1.0 (/root/hello_world)
error: linker `cc` not found
|
= note: No such file or directory (os error 2)

error: could not compile `hello_world` (bin "hello_world") due to previous error

在Debian环境下,使用APT包管理器安装编译器的简易方式如下

1
$ apt install build-essential

大功告成

构建Rust应用程序。

1
2
3
$ cargo build
Compiling hello_world v0.1.0 (/root/hello_world)
Finished dev [unoptimized + debuginfo] target(s) in 0.27s

执行应用程序。

1
2
3
4
$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.00s
Running `target/debug/hello_world`
Hello, world!

你好Rust世界!