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

相关推荐
sunxunyong2 小时前
CGroup配置
linux·运维·服务器
hy____1233 小时前
Linux_网络编程套接字
linux·运维·网络
若风的雨3 小时前
【deepseek】 Linux 调度延时分析
linux
2301_803554524 小时前
linux 以及 c++编程里对于进程,线程的操作
linux·运维·c++
LuDvei4 小时前
windows 中 vs code远程连接linux
linux·运维·服务器·windows
GDAL4 小时前
MANIFEST.in简介
linux·服务器·前端·python
点点滴滴的记录4 小时前
Redis部署在Linux上性能高于Windows
linux·数据库·redis
蜕变的小白5 小时前
基于Linux的天气查询项目
linux·运维·服务器
卤炖阑尾炎5 小时前
Linux firewalld 防火墙从入门到精通:原理与配置全解析
linux·运维·php
小云数据库服务专线5 小时前
linux grep命令
linux·运维·服务器