第一个Rust程序

在安装好Rust以后,我们就可以编写程序了。

首先,我们执行下面的命令,尽量让你的rust版本和我的版本相同,或者比我的版本大。

bash 复制代码
zhangdapeng@zhangdapeng:~$ cargo --version
cargo 1.78.0 (54d8815d0 2024-03-26)
zhangdapeng@zhangdapeng:~$ rustc --version
rustc 1.78.0 (9b00956e5 2024-04-29)
zhangdapeng@zhangdapeng:~$ rustdoc --version
rustdoc 1.78.0 (9b00956e5 2024-04-29)
zhangdapeng@zhangdapeng:~$

cargo是编译管理器,包管理器和通用工具。

rustc是rust编译器。

rustdoc是rust的文档工具。

使用cargo创建一个新的项目:

bash 复制代码
zhangdapeng@zhangdapeng:~/code/rust$ cargo new hello
    Creating binary (application) `hello` package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

查看创建的hello包的目录:

bash 复制代码
zhangdapeng@zhangdapeng:~/code/rust$ ls hello
Cargo.toml  src

在Cargo.toml中,保存了这个项目的基本信息:

bash 复制代码
zhangdapeng@zhangdapeng:~/code/rust$ cd hello
zhangdapeng@zhangdapeng:~/code/rust/hello$ cat Cargo.toml 
[package]
name = "hello"
version = "0.1.0"
edition = "2021"

[dependencies]

在src中,存放的是代码文件,这里面本身已经有一个基本的代码文件了:

bash 复制代码
zhangdapeng@zhangdapeng:~/code/rust/hello$ ls -lah src
total 12K
drwxrwxr-x 2 zhangdapeng zhangdapeng 4.0K May 14 09:19 .
drwxrwxr-x 4 zhangdapeng zhangdapeng 4.0K May 14 09:19 ..
-rw-rw-r-- 1 zhangdapeng zhangdapeng   45 May 14 09:19 main.rs
zhangdapeng@zhangdapeng:~/code/rust/hello$ cat src/main.rs 
fn main() {
    println!("Hello, world!");
}

因为已经有一个基础代码了,所以我们可以直接将整个项目运行起来:

bash 复制代码
zhangdapeng@zhangdapeng:~/code/rust/hello$ cargo run
   Compiling hello v0.1.0 (/home/zhangdapeng/code/rust/hello)
error: linker `cc` not found
  |
  = note: No such file or directory (os error 2)

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

但是,我这里遇到了一个错误。

我的系统版本是Ubuntu24,这里的错误信息是:error: linker cc not found

这里的错误可以通过安装gcc来解决:

bash 复制代码
zhangdapeng@zhangdapeng:~/code/rust/hello$ sudo apt install gcc -y
[sudo] password for zhangdapeng: 
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  binutils binutils-common binutils-x86-64-linux-gnu cpp cpp-13 cpp-13-x86-64-linux-gnu cpp-x86-64-linux-gnu gcc-13 gcc-13-base gcc-13-x86-64-linux-gnu gcc-x86-64-linux-gnu libasan8 libatomic1
  libbinutils libcc1-0 libctf-nobfd0 libctf0 libgcc-13-dev libgomp1 libgprofng0 libhwasan0 libisl23 libitm1 liblsan0 libmpc3 libquadmath0 libsframe1 libtsan2 libubsan1
Suggested packages:
  binutils-doc gprofng-gui cpp-doc gcc-13-locales cpp-13-doc gcc-multilib make autoconf automake libtool flex bison gdb gcc-doc gcc-13-multilib gcc-13-doc gdb-x86-64-linux-gnu
The following NEW packages will be installed:
  binutils binutils-common binutils-x86-64-linux-gnu cpp cpp-13 cpp-13-x86-64-linux-gnu cpp-x86-64-linux-gnu gcc gcc-13 gcc-13-base gcc-13-x86-64-linux-gnu gcc-x86-64-linux-gnu libasan8
  libatomic1 libbinutils libcc1-0 libctf-nobfd0 libctf0 libgcc-13-dev libgomp1 libgprofng0 libhwasan0 libisl23 libitm1 liblsan0 libmpc3 libquadmath0 libsframe1 libtsan2 libubsan1
0 upgraded, 30 newly installed, 0 to remove and 4 not upgraded.
Need to get 51.6 MB of archives.
After this operation, 182 MB of additional disk space will be used.
Get:1 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/main amd64 binutils-common amd64 2.42-4ubuntu2 [239 kB]
Get:2 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/main amd64 libsframe1 amd64 2.42-4ubuntu2 [14.8 kB]
Get:3 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/main amd64 libbinutils amd64 2.42-4ubuntu2 [572 kB]
Get:4 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/main amd64 libctf-nobfd0 amd64 2.42-4ubuntu2 [97.1 kB]
Get:5 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/main amd64 libctf0 amd64 2.42-4ubuntu2 [94.5 kB]
Get:6 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/main amd64 libgprofng0 amd64 2.42-4ubuntu2 [851 kB]
Get:7 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/main amd64 binutils-x86-64-linux-gnu amd64 2.42-4ubuntu2 [2,469 kB]
Get:8 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/main amd64 binutils amd64 2.42-4ubuntu2 [18.0 kB]
Get:9 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/main amd64 gcc-13-base amd64 13.2.0-23ubuntu4 [49.0 kB]
Get:10 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/main amd64 libisl23 amd64 0.26-3build1 [680 kB]
Get:11 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/main amd64 libmpc3 amd64 1.3.1-1build1 [54.5 kB]
Get:12 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/main amd64 cpp-13-x86-64-linux-gnu amd64 13.2.0-23ubuntu4 [11.2 MB]
Get:13 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/main amd64 cpp-13 amd64 13.2.0-23ubuntu4 [1,032 B]
Get:14 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/main amd64 cpp-x86-64-linux-gnu amd64 4:13.2.0-7ubuntu1 [5,326 B]
Get:15 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/main amd64 cpp amd64 4:13.2.0-7ubuntu1 [22.4 kB]
Get:16 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/main amd64 libcc1-0 amd64 14-20240412-0ubuntu1 [47.7 kB]
Get:17 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/main amd64 libgomp1 amd64 14-20240412-0ubuntu1 [147 kB]
Get:18 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/main amd64 libitm1 amd64 14-20240412-0ubuntu1 [28.9 kB]
Get:19 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/main amd64 libatomic1 amd64 14-20240412-0ubuntu1 [10.4 kB]
Get:20 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/main amd64 libasan8 amd64 14-20240412-0ubuntu1 [3,024 kB]
Get:21 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/main amd64 liblsan0 amd64 14-20240412-0ubuntu1 [1,313 kB]
Get:22 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/main amd64 libtsan2 amd64 14-20240412-0ubuntu1 [2,736 kB]
Get:23 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/main amd64 libubsan1 amd64 14-20240412-0ubuntu1 [1,175 kB]
Get:24 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/main amd64 libhwasan0 amd64 14-20240412-0ubuntu1 [1,632 kB]
Get:25 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/main amd64 libquadmath0 amd64 14-20240412-0ubuntu1 [153 kB]
Get:26 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/main amd64 libgcc-13-dev amd64 13.2.0-23ubuntu4 [2,688 kB]
Get:27 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/main amd64 gcc-13-x86-64-linux-gnu amd64 13.2.0-23ubuntu4 [21.9 MB]
Get:28 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/main amd64 gcc-13 amd64 13.2.0-23ubuntu4 [482 kB]
Get:29 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/main amd64 gcc-x86-64-linux-gnu amd64 4:13.2.0-7ubuntu1 [1,212 B]
Get:30 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/main amd64 gcc amd64 4:13.2.0-7ubuntu1 [5,018 B]
Fetched 51.6 MB in 4s (12.8 MB/s)
Selecting previously unselected package binutils-common:amd64.
(Reading database ... 83311 files and directories currently installed.)
Preparing to unpack .../00-binutils-common_2.42-4ubuntu2_amd64.deb ...
Unpacking binutils-common:amd64 (2.42-4ubuntu2) ...
Selecting previously unselected package libsframe1:amd64.
Preparing to unpack .../01-libsframe1_2.42-4ubuntu2_amd64.deb ...
Unpacking libsframe1:amd64 (2.42-4ubuntu2) ...
Selecting previously unselected package libbinutils:amd64.
Preparing to unpack .../02-libbinutils_2.42-4ubuntu2_amd64.deb ...
Unpacking libbinutils:amd64 (2.42-4ubuntu2) ...
Selecting previously unselected package libctf-nobfd0:amd64.
Preparing to unpack .../03-libctf-nobfd0_2.42-4ubuntu2_amd64.deb ...
Unpacking libctf-nobfd0:amd64 (2.42-4ubuntu2) ...
Selecting previously unselected package libctf0:amd64.
Preparing to unpack .../04-libctf0_2.42-4ubuntu2_amd64.deb ...
Unpacking libctf0:amd64 (2.42-4ubuntu2) ...
Selecting previously unselected package libgprofng0:amd64.
Preparing to unpack .../05-libgprofng0_2.42-4ubuntu2_amd64.deb ...
Unpacking libgprofng0:amd64 (2.42-4ubuntu2) ...
Selecting previously unselected package binutils-x86-64-linux-gnu.
Preparing to unpack .../06-binutils-x86-64-linux-gnu_2.42-4ubuntu2_amd64.deb ...
Unpacking binutils-x86-64-linux-gnu (2.42-4ubuntu2) ...
Selecting previously unselected package binutils.
Preparing to unpack .../07-binutils_2.42-4ubuntu2_amd64.deb ...
Unpacking binutils (2.42-4ubuntu2) ...
Selecting previously unselected package gcc-13-base:amd64.
Preparing to unpack .../08-gcc-13-base_13.2.0-23ubuntu4_amd64.deb ...
Unpacking gcc-13-base:amd64 (13.2.0-23ubuntu4) ...
Selecting previously unselected package libisl23:amd64.
Preparing to unpack .../09-libisl23_0.26-3build1_amd64.deb ...
Unpacking libisl23:amd64 (0.26-3build1) ...
Selecting previously unselected package libmpc3:amd64.
Preparing to unpack .../10-libmpc3_1.3.1-1build1_amd64.deb ...
Unpacking libmpc3:amd64 (1.3.1-1build1) ...
Selecting previously unselected package cpp-13-x86-64-linux-gnu.
Preparing to unpack .../11-cpp-13-x86-64-linux-gnu_13.2.0-23ubuntu4_amd64.deb ...
Unpacking cpp-13-x86-64-linux-gnu (13.2.0-23ubuntu4) ...
Selecting previously unselected package cpp-13.
Preparing to unpack .../12-cpp-13_13.2.0-23ubuntu4_amd64.deb ...
Unpacking cpp-13 (13.2.0-23ubuntu4) ...
Selecting previously unselected package cpp-x86-64-linux-gnu.
Preparing to unpack .../13-cpp-x86-64-linux-gnu_4%3a13.2.0-7ubuntu1_amd64.deb ...
Unpacking cpp-x86-64-linux-gnu (4:13.2.0-7ubuntu1) ...
Selecting previously unselected package cpp.
Preparing to unpack .../14-cpp_4%3a13.2.0-7ubuntu1_amd64.deb ...
Unpacking cpp (4:13.2.0-7ubuntu1) ...
Selecting previously unselected package libcc1-0:amd64.
Preparing to unpack .../15-libcc1-0_14-20240412-0ubuntu1_amd64.deb ...
Unpacking libcc1-0:amd64 (14-20240412-0ubuntu1) ...
Selecting previously unselected package libgomp1:amd64.
Preparing to unpack .../16-libgomp1_14-20240412-0ubuntu1_amd64.deb ...
Unpacking libgomp1:amd64 (14-20240412-0ubuntu1) ...
Selecting previously unselected package libitm1:amd64.
Preparing to unpack .../17-libitm1_14-20240412-0ubuntu1_amd64.deb ...
Unpacking libitm1:amd64 (14-20240412-0ubuntu1) ...
Selecting previously unselected package libatomic1:amd64.
Preparing to unpack .../18-libatomic1_14-20240412-0ubuntu1_amd64.deb ...
Unpacking libatomic1:amd64 (14-20240412-0ubuntu1) ...
Selecting previously unselected package libasan8:amd64.
Preparing to unpack .../19-libasan8_14-20240412-0ubuntu1_amd64.deb ...
Unpacking libasan8:amd64 (14-20240412-0ubuntu1) ...
Selecting previously unselected package liblsan0:amd64.
Preparing to unpack .../20-liblsan0_14-20240412-0ubuntu1_amd64.deb ...
Unpacking liblsan0:amd64 (14-20240412-0ubuntu1) ...
Selecting previously unselected package libtsan2:amd64.
Preparing to unpack .../21-libtsan2_14-20240412-0ubuntu1_amd64.deb ...
Unpacking libtsan2:amd64 (14-20240412-0ubuntu1) ...
Selecting previously unselected package libubsan1:amd64.
Preparing to unpack .../22-libubsan1_14-20240412-0ubuntu1_amd64.deb ...
Unpacking libubsan1:amd64 (14-20240412-0ubuntu1) ...
Selecting previously unselected package libhwasan0:amd64.
Preparing to unpack .../23-libhwasan0_14-20240412-0ubuntu1_amd64.deb ...
Unpacking libhwasan0:amd64 (14-20240412-0ubuntu1) ...
Selecting previously unselected package libquadmath0:amd64.
Preparing to unpack .../24-libquadmath0_14-20240412-0ubuntu1_amd64.deb ...
Unpacking libquadmath0:amd64 (14-20240412-0ubuntu1) ...
Selecting previously unselected package libgcc-13-dev:amd64.
Preparing to unpack .../25-libgcc-13-dev_13.2.0-23ubuntu4_amd64.deb ...
Unpacking libgcc-13-dev:amd64 (13.2.0-23ubuntu4) ...
Selecting previously unselected package gcc-13-x86-64-linux-gnu.
Preparing to unpack .../26-gcc-13-x86-64-linux-gnu_13.2.0-23ubuntu4_amd64.deb ...
Unpacking gcc-13-x86-64-linux-gnu (13.2.0-23ubuntu4) ...
Selecting previously unselected package gcc-13.
Preparing to unpack .../27-gcc-13_13.2.0-23ubuntu4_amd64.deb ...
Unpacking gcc-13 (13.2.0-23ubuntu4) ...
Selecting previously unselected package gcc-x86-64-linux-gnu.
Preparing to unpack .../28-gcc-x86-64-linux-gnu_4%3a13.2.0-7ubuntu1_amd64.deb ...
Unpacking gcc-x86-64-linux-gnu (4:13.2.0-7ubuntu1) ...
Selecting previously unselected package gcc.
Preparing to unpack .../29-gcc_4%3a13.2.0-7ubuntu1_amd64.deb ...
Unpacking gcc (4:13.2.0-7ubuntu1) ...
Setting up binutils-common:amd64 (2.42-4ubuntu2) ...
Setting up libctf-nobfd0:amd64 (2.42-4ubuntu2) ...
Setting up libgomp1:amd64 (14-20240412-0ubuntu1) ...
Setting up libsframe1:amd64 (2.42-4ubuntu2) ...
Setting up gcc-13-base:amd64 (13.2.0-23ubuntu4) ...
Setting up libquadmath0:amd64 (14-20240412-0ubuntu1) ...
Setting up libmpc3:amd64 (1.3.1-1build1) ...
Setting up libatomic1:amd64 (14-20240412-0ubuntu1) ...
Setting up libubsan1:amd64 (14-20240412-0ubuntu1) ...
Setting up libhwasan0:amd64 (14-20240412-0ubuntu1) ...
Setting up libasan8:amd64 (14-20240412-0ubuntu1) ...
Setting up libtsan2:amd64 (14-20240412-0ubuntu1) ...
Setting up libbinutils:amd64 (2.42-4ubuntu2) ...
Setting up libisl23:amd64 (0.26-3build1) ...
Setting up libcc1-0:amd64 (14-20240412-0ubuntu1) ...
Setting up liblsan0:amd64 (14-20240412-0ubuntu1) ...
Setting up libitm1:amd64 (14-20240412-0ubuntu1) ...
Setting up libctf0:amd64 (2.42-4ubuntu2) ...
Setting up cpp-13-x86-64-linux-gnu (13.2.0-23ubuntu4) ...
Setting up libgprofng0:amd64 (2.42-4ubuntu2) ...
Setting up libgcc-13-dev:amd64 (13.2.0-23ubuntu4) ...
Setting up binutils-x86-64-linux-gnu (2.42-4ubuntu2) ...
Setting up cpp-x86-64-linux-gnu (4:13.2.0-7ubuntu1) ...
Setting up cpp-13 (13.2.0-23ubuntu4) ...
Setting up gcc-13-x86-64-linux-gnu (13.2.0-23ubuntu4) ...
Setting up binutils (2.42-4ubuntu2) ...
Setting up gcc-13 (13.2.0-23ubuntu4) ...
Setting up cpp (4:13.2.0-7ubuntu1) ...
Setting up gcc-x86-64-linux-gnu (4:13.2.0-7ubuntu1) ...
Setting up gcc (4:13.2.0-7ubuntu1) ...
Processing triggers for man-db (2.12.0-4build2) ...
Processing triggers for libc-bin (2.39-0ubuntu8.1) ...
Scanning processes...                                                                                                                                                                               
Scanning linux images...                                                                                                                                                                            

Running kernel seems to be up-to-date.

No services need to be restarted.

No containers need to be restarted.

No user sessions are running outdated binaries.

No VM guests are running outdated hypervisor (qemu) binaries on this host.

安装了gcc以后,重新运行就可以了:

bash 复制代码
zhangdapeng@zhangdapeng:~/code/rust/hello$ cargo run
   Compiling hello v0.1.0 (/home/zhangdapeng/code/rust/hello)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.37s
     Running `target/debug/hello`
Hello, world!

这个命令会生成一个可执行文件:

bash 复制代码
zhangdapeng@zhangdapeng:~/code/rust/hello$ ls target/debug/
build  deps  examples  hello  hello.d  incremental

我们也可以直接调用可执行文件运行:

bash 复制代码
zhangdapeng@zhangdapeng:~/code/rust/hello$ ./target/debug/hello 
Hello, world!

cargo还贴心的给我们准备了清理调试代码的功能:

bash 复制代码
zhangdapeng@zhangdapeng:~/code/rust/hello$ cargo clean
     Removed 21 files, 7.3MiB total
zhangdapeng@zhangdapeng:~/code/rust/hello$ ls -lah
total 28K
drwxrwxr-x 4 zhangdapeng zhangdapeng 4.0K May 14 09:34 .
drwxrwxr-x 3 zhangdapeng zhangdapeng 4.0K May 14 09:19 ..
-rw-rw-r-- 1 zhangdapeng zhangdapeng  149 May 14 09:24 Cargo.lock
-rw-rw-r-- 1 zhangdapeng zhangdapeng   76 May 14 09:19 Cargo.toml
drwxrwxr-x 6 zhangdapeng zhangdapeng 4.0K May 14 09:19 .git
-rw-rw-r-- 1 zhangdapeng zhangdapeng    8 May 14 09:19 .gitignore
drwxrwxr-x 2 zhangdapeng zhangdapeng 4.0K May 14 09:19 src
相关推荐
计算机相关知识分享6 分钟前
python基础知识(二)
开发语言·python
Python私教35 分钟前
PyPika:Python SQL 查询构建器
开发语言·python·sql
全栈老实人_37 分钟前
考研互学互助系统|Java|SSM|VUE| 前后端分离
java·开发语言·tomcat·maven
天天进步201541 分钟前
Java全栈项目实战:校园报修服务系统
java·开发语言
Themberfue1 小时前
Java 网络原理 ①-IO多路复用 || 自定义协议 || XML || JSON
xml·java·开发语言·网络·计算机网络·json
m0_699659561 小时前
DAY3 QT简易登陆界面优化
开发语言·qt·命令模式
励志成为大佬的小杨2 小时前
关键字初级学习
c语言·开发语言·算法
疯一样的码农2 小时前
基于Spring Boot + Vue3实现的在线商品竞拍管理系统源码+文档
java·spring boot·后端
Y编程小白3 小时前
SpringBoot的pom.xml文件中,scope标签有几种配置?
xml·spring boot·后端
szpc16213 小时前
100V宽压输入反激隔离电源,适用于N道沟MOSFET或GaN或5V栅极驱动器,无需光耦合
c语言·开发语言·人工智能·单片机·嵌入式硬件·生成对抗网络·fpga开发