some_good_stuff icon indicating copy to clipboard operation
some_good_stuff copied to clipboard

Rust 安装与配置

Open chuyik opened this issue 6 years ago • 0 comments

导航

  • 配置中科大镜像
  • 安装 Rust
  • 安装 VSCode 插件
  • 如何编译项目
  • 如何格式化代码
  • 如何安装项目依赖

配置中科大镜像

  1. 配置 rustup 镜像

来源:https://lug.ustc.edu.cn/wiki/mirrors/help/rust-static?s[]=rust

export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup
  1. 配置 Rust Crates 镜像

来源:https://lug.ustc.edu.cn/wiki/mirrors/help/rust-crates

创建 ~/.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

rustup 是 Rust 官方的版本管理工具,是安装 Rust 的首选。

curl https://sh.rustup.rs -sSf | sh

运行该命令后,rustup 会安装 stable 版本的 Rust、Cargo。

↥ 返回顶部

安装 VSCode 插件

建议安装 Rust(rls) 插件,但安装过程可能会报错,请根据以下方法解决:

提示:nightly toolchain not installed. Install?

安装 nightly 版本或者强制使用 stable 版本即可。

方法一:命令行安装 nightly 版本

rustup toolchain install nightly

方法二:修改 VSCode settings,设置使用 stable 版本

{ "rust-client.channel": "stable" }

提示 Could not install RLS component

rustup 安装缺失的组件即可。

rustup component add \
  rust-analysis \
  rust-src \
  rls-preview \
  --toolchain stable|nightly

提示: the cargo feature edition requires a nightly version of Cargo, but this is the stable channel

cargo 也改用 nightly 版本即可。

rustup override set nightly

↥ 返回顶部

如何编译项目

方法一:使用 rustc 编译

rustc main.rs && ./main

方法二:使用 cargo 编译

cargo new hello_world # 或者 cargo init
cargo build && cargo run

方法三:监听代码变化并自动编译

cargo install cargo-watch
RUSTFLAGS=-Awarnings cargo watch -x run -w ./src

↥ 返回顶部

如何格式化代码

rustup component add rustfmt-preview

# 安装后就会有 fmt 命令
cargo fmt

↥ 返回顶部

如何安装项目依赖

cargo 官方没有提供类似于 npm install 的方法,需要使用第三方组件。

cargo install cargo-edit

# 安装后就会有 add/rm/upgrade 三个命令
# cargo add
# cargo rm
# cargo upgrade

↥ 返回顶部

chuyik avatar Aug 02 '18 03:08 chuyik