Ubuntu 22.04安装Rust编译环境并且测试

我参考的博客是《Rust使用国内Crates 源、 rustup源 |字节跳动新的 Rust 镜像源以及安装rust》
lsb_release -r看到操作系统版本是22.04,uname -r看到内核版本是uname -r

sudo apt install -y gcc先安装gcc,要是结果给我的一样的话,那么就是安装好了gcc

sudo vim /etc/profile把下边的内容填写进去:

shell 复制代码
export RUSTUP_DIST_SERVER="https://rsproxy.cn"
export RUSTUP_UPDATE_ROOT="https://rsproxy.cn/rustup"

sudo tail -n 2 /etc/profile看一下最后2行的内容,source /etc/profile使环境变量生效。

环境变量RUSTUP_DIST_SERVER用于更新toolchainRUSTUP_UPDATE_ROOT用于更新rustup。下边有几个备用选项。

shell 复制代码
# 清华大学
RUSTUP_DIST_SERVER=https://mirrors.tuna.tsinghua.edu.cn/rustup

# 中国科学技术大学
RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup

# 上海交通大学
RUSTUP_DIST_SERVER=https://mirrors.sjtug.sjtu.edu.cn/rust-static/

sudo curl --proto '=https' --tlsv1.2 -sSf https://rsproxy.cn/rustup-init.sh | sh进行安装。

完成之后,如下图:

source "$HOME/.cargo/env"使环境变量生效。

sudo vim ~/.cargo/config设置一下crates.io 镜像,把下边的内容填进去:

shell 复制代码
[source.crates-io]
replace-with = 'rsproxy-sparse'
[source.rsproxy]
registry = "https://rsproxy.cn/crates.io-index"
[source.rsproxy-sparse]
registry = "sparse+https://rsproxy.cn/index/"
[registries.rsproxy]
index = "https://rsproxy.cn/crates.io-index"
[net]
git-fetch-with-cli = true

sudo cat ~/.cargo/config可以看到填入的信息。

cargo new rustTest创建二进制程序,cd rustTest进入源代码目录下。

ls -lR看一下当前目录下的内容。

可以看到当前目录下有一个Cargo.toml文件和src目录,而src目录下有main.rs文件,就是相当于如下所示:

shell 复制代码
├── Cargo.toml
└── src/
    └── main.rs

Cargo.toml的最后填上内容rand = "0.6.5",最后Cargo.toml里边的内容如下:

shell 复制代码
[package]
name = "rustTest"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rand = "0.6.5"


sudo vim src/main.rs把内容修改如下:

rust 复制代码
extern crate rand;
use rand::Rng;
fn main() {
   let num = rand::thread_rng().gen_range(0, 100);
    println!("生成在0(包括)到100(包括)之间的数:{}", num);
}

然后在当前rustTest目录下,使用cargo run进行编译运行,这次运行的输出内容是生成在0(包括)到100(包括)之间的数:84

完成测试。

此文章为11月Day 18学习笔记,内容来源于极客时间《Rust 语言从入门到实战》

相关推荐
岁月不能老4 分钟前
Linux-Part8-考试(学习Linux第8天)
linux·运维·学习
苏近之7 分钟前
深入理解 Rust 中的生命周期
rust
ImAlex41 分钟前
如何使用gcc的-finstrument-functions特性通过打印函数调用栈辅助理解复杂C/C++项目的函数调用关系
linux·c语言
马大胡子1 小时前
Greenbone(绿骨)开源GVM容器docker部署和汉化介绍
linux·网络安全·docker
用户217516114381 小时前
【Linux】软硬连接与动静态库
linux
UestcXiye1 小时前
Rust 学习笔记:编程练习(一)
rust
奶油话梅糖2 小时前
LS-Linux-004 误删 Python 和 yum、dnf 后的恢复步骤
linux
和煦的春风2 小时前
案例分析 | SurfaceFlinger 大片Runnable引起的卡顿
android·linux
涵信3 小时前
第十二节:性能优化高频题-shallowRef/shallowReactive使用场景
linux·ubuntu·性能优化
老秦包你会3 小时前
Linux课程五课---Linux进程认识1
linux·运维·服务器