Rust 开发环境安装

1.安装

1.1. 设置根路径

设置环境变量RUST_HOME;
RUSTUP_HOME=%RUST_HOME%\.rustup
CARGO_HOME=%RUST_HOME%\.cargo
RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup
RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
RUST_BACKTRACE=1 //调试出错时打印详细调用栈

1.2.安装软件版本

下载rustup-init.exe,在cmd中运行命令,选择1
安装完成后,运行rustup component add rls(否则Eclipse Corrosion中会提示Install missing rls component in Rustup)
在%CARGO_HOME%下创建config文件,内容如下,清华的镜像比较完整,中科大的非常不稳定。
[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"

# 替换成你偏好的镜像源
# replace-with = 'sjtu'
replace-with = 'tuna'

# 清华大学
[source.tuna]
registry = "http://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"

# 中国科学技术大学
[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"

# 上海交通大学
[source.sjtu]
registry = "https://mirrors.sjtug.sjtu.edu.cn/git/crates.io-index"

# rustcc社区
[source.rustcc]
registry = "git://crates.rustcc.cn/crates.io-index"


1.3.安装rls

用于提示、自动完成等功能
rustup component add rls rust-analysis rust-src

1.4.创建工程

Cargo new 工程名称

1.5.测试运行

在项目下运行cargo build会按照cargo.toml中的设置,开始编译
运行cargo run,就会运行,默认运行debug版,性能会比较弱,用cargo run --release会大幅度提升性能,注意后面的是“--release”,不能省掉“--”

1.6.Blocking错误

如果编译被异常终止,导致cargo进程被吊死,不能释放.cargo/.package-cache。这种情况下首先要停止所有的cargo进程,同时删除.cargo/.package-cache文件。
有时候,vscode也会锁住cargo build,还没找出规律。


2.VSCode开发环境

2.1.安装rust插件

在扩展中搜索rust,安装rust相关插件

2.2.调试

Windows下安装C\C++ for Visual Studio Code,其他OS安装CodeLLDB(没有亲自试过)。注意,Windows下是安装这个,否则不能单步跟踪。

手动修改.vscode/launch.json,type改为cppvsdbg,增加program,按F5就可以单步跟踪了。
 "configurations": [
  {
        "type": "cppvsdbg",
        "program": "${workspaceRoot}/target/debug/xxx.exe",
...
}

3.开发

3.1.注意事项

最后的返回值表达式,不能加分号

3.2.常用库

1.模板引擎:https://github.com/botika/yarte
2.日志:https://github.com/estk/log4rs
3.Web框架:https://github.com/http-rs/tide
4.Web 框架:https://chrismorgan.info/blog/introducing-teepee/ 从rust-http演变而来
5.Http3 Web框架:https://github.com/mozilla/neqo
6.Crypto库,基于BoringSSL:https://github.com/briansmith/ring
7.资源库:https://crates.io,整个rust生态的库都可以在这里搜索下载,初学者可以在这里了解生态已经有了哪些现成的开源部件。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值