ToB企服应用市场:ToB评测及商务社交产业平台

标题: 00 - Rust 开发环境设置 [打印本页]

作者: 石小疯    时间: 2023-9-30 17:11
标题: 00 - Rust 开发环境设置
目录

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 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 安装这两个插件
其它推荐插件:
使用 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. }
复制代码
调试界面如下:


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




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4