前端 Rust Develop(Learn) Environment (macOS/windows)

ninibunny · December 16, 2019 · 42 hits

两种安装方式

传统方式安装 rustup方式安装 (多版本管理 stable / beta / nightly)

ready

配置

预先设置环境变量

export rust_home=${local}/rust
export rustup_home=${volumes_sd}/repos/rustup
export rust_src_path=${rustup_home}/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/src
export cargo_home=${volumes_sd}/repos/cargo # cargo 本地包仓库目录
export path=$rust_home/bin:$cargo_home/bin:$path

安装rustup

curl https://sh.rustup.rs -ssf | sh -s -- 
    --no-modify-path 
    --default-toolchain nightly 
    -y
    
curl --proto '=https' --tlsv1.2 -ssf https://sh.rustup.rs | sh -s -- 
    --no-modify-path 
    --default-toolchain nightly 
    -y
rustup-init.exe --no-modify-path --default-toolchain nightly  -y

输出提示信息 cargo的安装目录在预先设置的环境变量中已经配置完成
添加oh-my-zsh插件

rustup completions zsh > /users/yakir/.oh-my-zsh/custom/plugins/rustup/_rustup
# 在 ~/.zshrc 的plugins列表中添加 `rustup`

rustup 基础使用

🙈 详见官方

  • 安装不同通道的rust
    rustup install nightly
    rustup install beta
    rustup install stable
    
  • 更新 rust 和 rutup
    rustup update && 
      rustup self update
    
  • 指定运行特定的通道 (版本)
rustup run stable cargo --version
rustup run nightly rustc --version

cargo +beta --version
cargo +nightly --version

通过 rustup 安装组件

# nightly
rustup component add rust-src --toolchain nightly
rustup component add rls --toolchain nightly
rustup component add rust-analysis --toolchain nightly
rustup component add rustfmt --toolchain nightly
rustup component add clippy --toolchain nightly
rustup component add miri --toolchain nightly

#beta
rustup component add rust-src --toolchain beta
rustup component add rls --toolchain beta
rustup component add rust-analysis --toolchain beta
rustup component add rustfmt --toolchain beta
rustup component add clippy --toolchain beta
rustup component add miri --toolchain beta

# stable
rustup component add rust-src --toolchain stable
rustup component add rls --toolchain stable
rustup component add rust-analysis --toolchain stable
rustup component add rustfmt --toolchain stable
rustup component add clippy --toolchain stable
rustup component add miri --toolchain stable
# 运行clippy
cargo run --bin cargo-clippy --manifest-path=path_to_clippys_cargo.toml

alias

为了偷懒👽

alias rnr="rustup run nightly"
alias rsr="rustup run stable"
alias rbr="rustup run beta"
alias rsu="rustup self update"
alias ru="rustup update" 

测试 rust 可用

// hello.rs
fn main() {
    println!("hello");
}

编译 & 运行

rustc hello.rs
./hello 
# hello

测试 cargo 可用

cargo init --bin honey
#      created binary (application) project
cd honey
cargo run
#   compiling honey v0.1.0 (file:///users/yakir/think/honey)
#    finished dev [unoptimized + debuginfo] target(s) in 1.18 secs
#     running `target/debug/honey`
#hello, world!

组件安装

  • 安装方式一
cargo install -f -j 6 --all-features -v racer mdbook cargo-src fd-find
strip xxx....
  • 安装方式二 (源码安装)
git clone --depth 1 git@github.com:racer-rust/racer.git

cd racer && cargo build --release # 编译之后二进制包在 ./target/release/racer

测试 racer 可用

racer complete std::io::b

# 输出类似信息
match bufread,1551,10,/users/yakir/local/rust/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/src/libstd/io/mod.rs,trait,pub trait bufread: read
match bytes,2171,11,/users/yakir/local/rust/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/src/libstd/io/mod.rs,struct,pub struct bytes<r>
match bufreader,51,11,/users/yakir/local/rust/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/src/libstd/io/buffered.rs,struct,pub struct bufreader<r>
match bufwriter,412,11,/users/yakir/local/rust/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/src/libstd/io/buffered.rs,struct,pub struct bufwriter<w: write>

编辑器支持

vim 整合

sublime text 整合

sublime text 3 package control安装, ctrl + ~启动控制台输入

import urllib.request,os,hashlib; h = '6f4c264a24d933ce70df5dedcf1dcaee' + 'ebe013ee18cced0ef93d5f746d80ef60'; pf = 'package control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.proxyhandler()) ); by = urllib.request.urlopen( 'http://packagecontrol.io/' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); print('error validating download (got %s instead of %s), please try manual install' % (dh, h)) if dh != h else open(os.path.join( ipp, pf), 'wb' ).write(by)
  1. 使用 package control 安装 rust enhanced  + shift + p 输入 install package 安装

  2. language server protocol clone 代码到 sublime packages 目录中 ${home}/library/application support/sublime text 3/packages  + shift + p 输入 satisfy dependencies 安装
    控制台输出 “package control: all dependencies have been satisfied” lsp 文档 lsp.readthedocs.io, docs

1. 组合一 - rustautocomplete - anaconda_rust

2. 组合二

工具集

  • amxml
  • bat
  • cargo-deps
  • cargo-lipo
  • cargo-modules
  • cargo-watch
  • cargo-web
  • cbindgen
  • evcxr
  • evcxr_repl
  • fblog
  • fd-find
  • ferris-says
  • grapple
  • hexyl
  • hyperfine
  • jql
  • jsonxf
  • lsd
  • mdbook
  • mdcat
  • qrencode
  • racer
  • rget
  • ripgrep
  • SS-rust
  • silicon
  • tealdeer
  • xterm256_converter

reference:
rustup.rs: https://github.com/rust-lang-nursery/rustup.rs

No Reply at the moment.
You need to Sign in before reply, if you don't have an account, please Sign up first.