00 - Rust 开发环境设置

打印 上一主题 下一主题

主题 907|帖子 907|积分 2721

目录

Rust 开发环境设置

0.1 安装 rust编译器及工具链

按如下脚本开始安装
  1. ## 配置国内镜像,提升下载速度
  2. echo "export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static" >> ~/.zshrc
  3. echo "export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup" >>  ~/.zshrc
  4. ## 配置生效
  5. source ~/.zshrc
  6. ## 获取并执行安装脚本
  7. ## 似乎通过下面获取并执行有点慢,使用下面获取脚本,并执行来完成
  8. # curl --proto '=https'  --tlsv1.2 -sSf https://sh.rustup.rs | sh
  9. curl --proto '=https'  --tlsv1.2 -sSf https://sh.rustup.rs >> rustup.sh
  10. sh rustup.sh
复制代码
选择1 执行默认安装
  1. Current installation options:
  2.    default host triple: x86_64-unknown-linux-gnu
  3.      default toolchain: stable (default)
  4.                profile: default
  5.   modify PATH variable: yes
  6. 1) Proceed with installation (default)
  7. 2) Customize installation
  8. 3) Cancel installation
  9. >1
复制代码
直到 Rust is installed now. Great! 表示安装成功!
  1. ## 将下面命令添加到 shell 启动文件中,使 Rust 相关命令执行文件生效
  2. source $HOME/.cargo/env
复制代码
  1. ## 查看版本
  2. ➜  ~ rustc --version
  3. rustc 1.64.0 (a55dd71d5 2022-09-19)
  4. ## 更新到最新版本
  5. ➜  ~ rustup update  
  6. info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
  7. info: checking for self-updates
  8.   stable-x86_64-unknown-linux-gnu unchanged - rustc 1.64.0 (a55dd71d5 2022-09-19)
  9. info: cleaning up downloads & tmp directories
  10. ## 卸载 Rust
  11. ➜  ~ rustup self uninstall
复制代码
更多 rustup 相关的使用可查其帮助文档。
0.2 Cargo

Cargo是Rust 内置的包管理和构建系统 ,在安装Rust时已经安装好,查看 cargo 版本
  1. ➜  ~ cargo --version
  2. cargo 1.64.0 (387270bc7 2022-09-16)
  3. ➜  ~ cargo -V      
  4. cargo 1.64.0 (387270bc7 2022-09-16)
复制代码
Cargo 的使用


  • 创建新项目:cargo new
  • 编译:cargo build
  • 运行:cargo run
  • 更新项目依赖:cargo update
  • 执行测试:cargo test
  • 生成文档:cargo doc
  • 静态检查:cargo check
  • cargo clippy: 类似eslint,lint工具检查代码可以优化的地方
  • cargo fmt: 类似go fmt,代码格式化
  • cargo tree: 查看第三方库的版本和依赖关系
  • cargo bench: 运行benchmark(基准测试,性能测试)
  • cargo udeps(第三方): 检查项目中未使用的依赖
另外 cargo build/run --release 使用 release 编译会比默认的 debug 编译性能提升 10 倍以上,但是 release 缺点是编译速度较慢,而且不会显示 panic backtrace 的具体行号。
更多Cargo相关的详情可去 The Cargo Book
构建可执行(Executable)项目
  1. ➜  rust git:(master) ✗ cargo new hello --bin
  2.      Created binary (application) `hello` package
  3. ➜  rust git:(master) ✗ cd hello && tree
  4. .
  5. ├── Cargo.toml
  6. └── src
  7.     └── main.rs
  8. 1 directory, 2 files
  9. ➜  hello git:(master) ✗ cargo run
  10.    Compiling hello v0.1.0 (/home/jfu/gitee/dev/code/rust/hello)
  11.     Finished dev [unoptimized + debuginfo] target(s) in 1.32s
  12.      Running `target/debug/hello`
  13. Hello, world!
复制代码
构建 Library 项目
  1. $ cargo new lib-demo --lib
复制代码
在后续的实战练习中详解。
0.3 vscode 配置 Rust 开发

主要是根据关键字  rls 和 Native Debug 安装这两个插件
其它推荐插件:

  • rust-analyzer:它会实时编译和分析你的 Rust 代码,提示代码中的错误,并对类型进行标注。你也可以使用官方的 rust 插件取代。
  • rust syntax:为代码提供语法高亮。
  • crates:帮助你分析当前项目的依赖是否是最新的版本。
  • better toml:Rust 使用 toml 做项目的配置管理。better toml 可以帮你语法高亮,并展示 toml 文件中的错误。
  • rust test lens:可以帮你快速运行某个 Rust 测试。
  • Tabnine:基于 AI 的自动补全,可以帮助你更快地撰写代码。
使用 VScode 上使用LLDB调试 Rust 程序

LLDB 是一款高性能调试器,更多详情去 LLDB 了解。
  1. C/C++ (Windows)
  2. CodeLLDB (OS X / Linux)
复制代码
根据开发平台安装上述调试插件后,Run >  Start Debugging 开启调试后,自动生成 launch.json文件,linux 平台如下:
  1. {
  2.     // Use IntelliSense to learn about possible attributes.
  3.     // Hover to view descriptions of existing attributes.
  4.     // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  5.     "version": "0.2.0",
  6.     "configurations": [
  7.         {
  8.             "type": "lldb",
  9.             "request": "launch",
  10.             "name": "Debug",
  11.             "program": "${workspaceFolder}/<executable file>",
  12.             "args": [],
  13.             "cwd": "${workspaceFolder}"
  14.         }
  15.     ]
  16. }
复制代码
在上述配置中的 program 字段中配置好编译出的可执行文件路径即可。
然后在 File > Preferences > Settings , 输入 break , 确保勾选 Debug: Allow Breakpoints EveryWhere,即可在源码中打断点。

开启调试了。
通过如下代码演示:
  1. fn main() {
  2.     let mut x = 5;
  3.     x += 2;
  4.     let mut y = 37;
  5.     let z = y / x;
  6.     let w = z + 8 * 4;
  7. }
复制代码
调试界面如下:


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

石小疯

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表