Windows 安装
去 rust 官网下载 rust-init.exe 然后见注意使用 git bash 而非 cmd 或 powershell。
~/.bash_profile
# RUSTUP_DIST_SERVER (用于更新 toolchain):
export RUSTUP_DIST_SERVER="https://rsproxy.cn"
# RUSTUP_UPDATE_ROOT (用于更新 rustup):
export RUSTUP_UPDATE_ROOT="https://rsproxy.cn/rustup"
# https://juejin.cn/post/7359893250188492863
export RUSTUP_HOME=/d/users/legend80s/rust/.rustup # 默认路径 ~/.rustup 或 %USERPROFILE%/.rustup
export CARGO_HOME=/d/users/legend80s/rust/.cargo # 默认路径 ~/.cargo 或 %USERPROFILE%/.cargo
如果 cargo -V 显示命令不存在
$ ll /d/users/legend80s/rust/.cargo/bin
$ echo $PATH
~/.bash_profile
export PATH=/d/users/legend80s/rust/.cargo/bin:$PATH
如果 cargo install xx 显示 gcc 不存在。安装 gcc bobbyhadz.com/blog/gcc-is…
估计10min。记得设置环境变量 /c/MinGW/bin。
------------ 阴阳割分晓 ------------
我们的目标是写一个 Rust Node.js Native Addon,即 Node.js 调用 rust 函数,解决 cpu intensive 任务。本文重点讲述如何顺利安装 Rust。
为什么需要镜像?因为国内安装使用官方镜像很可能超时。
could not download file from 'static.rust-lang.org/dist/
如何解决?
- 设置
# RUSTUP_DIST_SERVER (用于更新 toolchain):
export RUSTUP_DIST_SERVER="https://rsproxy.cn"
# RUSTUP_UPDATE_ROOT (用于更新 rustup):
export RUSTUP_UPDATE_ROOT="https://rsproxy.cn/rustup"
- 下载
export the env above first
curl --proto '=https' --tlsv1.2 -sSf https://rsproxy.cn/rustup-init.sh | sh
已经对比上述 rustup-init.sh 初始化和下载脚本和官方的除了空格其他都一样,可以放心下载。
看看安装是否成功(2024-02-29 14:59:02)
❯ rustc --version
rustc 1.76.0 (07dca489a 2024-02-04)
❯ cargo --version
cargo 1.76.0 (c84b36747 2024-01-18)
使用字节跳动提供的镜像下载 installer,其他源详见参考。
配置 crates.io 镜像,类似 npm 阿里镜像。如果需要引入 crate(类似 npm 包,cargo 等效于 npm)则需要配置,否则包下载会很慢。
.cargo/config.toml
[source.crates-io]
replace-with = 'rsproxy'
[source.rsproxy]
registry = "https://rsproxy.cn/crates.io-index"
[registries.rsproxy]
index = "https://rsproxy.cn/crates.io-index"
语法来自 rustwiki.org/en/cargo/re…
尝试第一个 hello world
cargo new hello_world
src/main.rs
fn main() {
println!('Hello, world! 你好,世界!')
}
cargo run
❯ cargo run
Compiling hello_world v0.1.0 (/Users/legend80s/playground/hello_world)
Finished dev [unoptimized + debuginfo] target(s) in 2.44s
Running `target/debug/hello_world`
Hello, world! 你好,世界!
第二课将引入 napi-rs。