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 语言从入门到实战》

相关推荐
冰橙子id2 小时前
linux-远程访问管理(sshd,scp,sftp)
linux·网络·ssh
光电的一只菜鸡3 小时前
ubuntu之坑(十五)——设备树
linux·数据库·ubuntu
saynaihe5 小时前
ubuntu 22.04 anaconda comfyui安装
linux·运维·服务器·ubuntu
鸠摩智首席音效师5 小时前
如何在 Ubuntu 上安装 Microsoft Edge 浏览器?
ubuntu·microsoft·edge
企鹅与蟒蛇5 小时前
Ubuntu-25.04 Wayland桌面环境安装Anaconda3之后无法启动anaconda-navigator问题解决
linux·运维·python·ubuntu·anaconda
小蜜蜂爱编程5 小时前
ubuntu透网方案
运维·服务器·ubuntu
程序设计实验室5 小时前
小心误关了NAS服务器!修改Linux的电源键功能
linux·nas
渡我白衣8 小时前
Linux操作系统之信号:信号的产生
linux
阿巴~阿巴~9 小时前
理解Linux文件系统:从物理存储到统一接口
linux·运维·服务器
tan77º9 小时前
【Linux网络编程】应用层自定义协议与序列化
linux·运维·服务器·网络·c++·tcp/ip