centos安装Rust

一条命令完成全部安装

echo "中途需要按回车键确认安装"; yum install -y gcc ;echo "export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static";echo "export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup";echo "export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static" >> /etc/profile;echo "export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup" >> /etc/profile;source  /etc/profile;curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh &&source $HOME/.cargo/env &&rustc -V &&echo "${HOME}/.cargo/config";echo "[source.crates-io]" ;echo 'registry = "https://github.com/rust-lang/crates.io-index"' ;echo "replace-with = 'ustc'";echo "[source.ustc]" ;echo 'registry = "git://mirrors.ustc.edu.cn/crates.io-index"' ;echo "[source.crates-io]" >>  ${HOME}/.cargo/config &&echo 'registry = "https://github.com/rust-lang/crates.io-index"' >>  ${HOME}/.cargo/config &&echo "replace-with = 'ustc'" >>  ${HOME}/.cargo/config &&echo "[source.ustc]" >>  ${HOME}/.cargo/config &&echo 'registry = "git://mirrors.ustc.edu.cn/crates.io-index"' >>  ${HOME}/.cargo/config

安装完成后,需要验证是否可用,请跳转到后边的: 使用Rust ,以验证

安装Rust

依赖准备

安装gcc

yum install -y gcc

换国内源下载

两条命令进行国内下载安装文件地址配置

vim /etc/profile

在最后填写如下内容

export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup

使得环境变量生效

source  /etc/profile

安装

使用命令安装,中间会有阻塞等待输入,直接按回车就是默认安装

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

配置环境变量

安装完成后,需要添加环境变量,才能生效

source $HOME/.cargo/env

安装完成检查

查看版本

rustc -V
# 输出 > rustc 1.77.1 (7cf61ebde 2024-03-27)

依赖包换源

vim ${HOME}/.cargo/config 

然后输入下边的内容来设置国内依赖包下载地址

[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
replace-with = 'ustc'
[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"

使用Rust

Hello world

vim helloworld.rs

输入内容:

fn main(){
        let test = "Hello, I love the world!";
        println!("{}",test);
}

编译运行

rustc helloworld.rs  # 编译
./helloworld # 运行编译后生成的文件

使用依赖包

创建程序

例如使用 random 的包;这需要创建一个二进制程序:

cargo new rustrandom
cd rustrandom

创建的 rustrandom 的目录结构是

.
├── Cargo.toml
└── src
    └── main.rs

添加包

vim Cargo.toml
在内容的最后添加:

rand = "0.6.5"

Cargo.toml

[package]
name = "rustrandom"
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"

使用包

编辑 rustrandom 目录下的 src/main.rs 文件
vim src/main.rs

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

运行

cargo run
首次运行会下载安装依赖包

 cargo run  
    Updating `ustc` index
  Downloaded autocfg v0.1.8 (registry `ustc`)
  Downloaded autocfg v1.2.0 (registry `ustc`)
  Downloaded rand_isaac v0.1.1 (registry `ustc`)
  Downloaded rand v0.6.5 (registry `ustc`)
  Downloaded rand_jitter v0.1.4 (registry `ustc`)
  Downloaded rand_core v0.3.1 (registry `ustc`)
  Downloaded libc v0.2.153 (registry `ustc`)
  Downloaded rand_pcg v0.1.2 (registry `ustc`)
  Downloaded rand_os v0.1.3 (registry `ustc`)
  Downloaded rand_chacha v0.1.1 (registry `ustc`)
  Downloaded rand_core v0.4.2 (registry `ustc`)
  Downloaded rand_xorshift v0.1.1 (registry `ustc`)
  Downloaded rand_hc v0.1.0 (registry `ustc`)
  Downloaded 13 crates (999.2 KB) in 1.19s
   Compiling autocfg v1.2.0
   Compiling rand_core v0.4.2
   Compiling autocfg v0.1.8
   Compiling rand_core v0.3.1
   Compiling libc v0.2.153
   Compiling rand_chacha v0.1.1
   Compiling rand_pcg v0.1.2
   Compiling rand v0.6.5
   Compiling rand_os v0.1.3
   Compiling rand_hc v0.1.0
   Compiling rand_xorshift v0.1.1
   Compiling rand_isaac v0.1.1
   Compiling rand_jitter v0.1.4
   Compiling rustrandom v0.1.0 (/root/rustrandom)
    Finished dev [unoptimized + debuginfo] target(s) in 4m 43s
     Running `target/debug/rustrandom`
生成在0(包括)到100(包括)之间的数:27

再次运行就不会下载了。

全部命令

echo "中途需要按回车键确认安装"; yum install -y gcc ;

echo "export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static";
echo "export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup";
echo "export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static" >> /etc/profile;
echo "export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup" >> /etc/profile;

source  /etc/profile;

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh &&

source $HOME/.cargo/env && 

rustc -V && 

echo "${HOME}/.cargo/config";


echo "[source.crates-io]" ;
echo 'registry = "https://github.com/rust-lang/crates.io-index"' ;
echo "replace-with = 'ustc'";
echo "[source.ustc]" ;
echo 'registry = "git://mirrors.ustc.edu.cn/crates.io-index"' ;
echo "[source.crates-io]" >>  ${HOME}/.cargo/config &&
echo 'registry = "https://github.com/rust-lang/crates.io-index"' >>  ${HOME}/.cargo/config &&
echo "replace-with = 'ustc'" >>  ${HOME}/.cargo/config &&
echo "[source.ustc]" >>  ${HOME}/.cargo/config &&
echo 'registry = "git://mirrors.ustc.edu.cn/crates.io-index"' >>  ${HOME}/.cargo/config 

相关推荐

  1. centos安装Rust

    2024-04-03 06:24:07       38 阅读
  2. Rust 安装

    2024-04-03 06:24:07       52 阅读
  3. Rust】——不安全Rust

    2024-04-03 06:24:07       25 阅读
  4. Rust】第一节:安装

    2024-04-03 06:24:07       54 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-03 06:24:07       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-03 06:24:07       101 阅读
  3. 在Django里面运行非项目文件

    2024-04-03 06:24:07       82 阅读
  4. Python语言-面向对象

    2024-04-03 06:24:07       91 阅读

热门阅读

  1. Android adb 常用命令

    2024-04-03 06:24:07       40 阅读
  2. 深入理解Spring Boot Controller层的作用与搭建过程

    2024-04-03 06:24:07       38 阅读
  3. el-table的复选框勾选整行变色

    2024-04-03 06:24:07       35 阅读
  4. SQL原理之Join算法详解(含伪代码算法示例)

    2024-04-03 06:24:07       36 阅读
  5. P2404 自然数的拆分问题

    2024-04-03 06:24:07       34 阅读
  6. 四、Mybatis-查询与删除

    2024-04-03 06:24:07       38 阅读
  7. Rust 的 termion 库控制终端光标的位置

    2024-04-03 06:24:07       42 阅读
  8. 亚远景科技-ASPICE评估目的

    2024-04-03 06:24:07       38 阅读
  9. LeetCode热题Hot100 - 正则表达式匹配

    2024-04-03 06:24:07       32 阅读
  10. 关于Mac配置逆向工程

    2024-04-03 06:24:07       35 阅读
  11. 力扣爆刷第110天之CodeTop100五连刷36-40

    2024-04-03 06:24:07       38 阅读