Rust

Rust #

VSCode扩展 #

安装必要的工具:

1apt update
2apt install build-essential

安装VSCode扩展:

  • rust-analyzer extension
  • CodeLLDB extension
  • Even Better TOML
  • crates
  • Rust Syntax
  • Coverage Gutters extension
  • Rust Test Explore

codelldb扩展包比较大,需要手动安装(注意不要再code扩展里点安装,保持codelldb未安装状态,使用下面的命令手动安装):

1wget https://github.com/vadimcn/codelldb/releases/download/v1.10.0/codelldb-x86_64-linux.vsix
2code-server -vvv --install-extension ./codelldb-x86_64-linux.vsix

本机Rust开发环境的配置 #

配置carte.io仓库使用国内镜像 #

配置cargo对于crate.io仓库使用国内代理镜像,创建或编辑~/.cargo/config:

 1[source.crates-io]
 2replace-with = 'rsproxy'
 3
 4[source.rsproxy]
 5registry = "https://rsproxy.cn/crates.io-index"
 6
 7[registries.rsproxy]
 8index = "https://rsproxy.cn/crates.io-index"
 9
10[net]
11git-fetch-with-cli = true

具体参考https://rsproxy.cn/

cargo edit #

安装cargo-edit:

1apt install libssl-dev
2apt-get install pkg-config
3cargo install cargo-edit

单元测试覆盖率 #

支持单元测试覆盖率显示:

  • cargo tarpaulin: 运行测试并分析你的Rust代码并提供代码覆盖率数据
  • cargo watch: 每次我们保存文件时,运行cargo tarpaulin
  • coverage gutters: 在VSCode中显示覆盖的代码行

安装cargo-tarpaulin:

1cargo install cargo-tarpaulin

cargo tarpaulin命令的更多参数详见

1cargo tarpaulin --out Lcov --output-dir target/tarpaulin

上面的命令在target目录里生成了lcov.info文件。

CMD+Shift+P输入Coverage Gutters: Watch输入后,会自动发现lcov.info文件,并在项目源码中显示测试覆盖率。

安装cargo-watch:

Cargo Watch监视你的项目的源代码是否有变化,并在变化发生时运行Cargo命令。

1cargo install cargo-watch
1cargo watch -x 'tarpaulin --out Lcov --output-dir target/tarpaulin'

性能优化:

1cargo watch -x 'tarpaulin --out Lcov --output-dir target/tarpaulin' -w src

参考链接 #

© 2024 青蛙小白
comments powered by Disqus