乐趣区

关于后端:使用cargo-edit管理Rust项目的依赖

介绍

大略因为 IDE 还不够 智能 & 弱小,在 Rust 中每次须要引入依赖时, 都须要手工增加到 Cargo.toml 文件中, 而且版本号还要去 crates 下面去查 (这个通过装置插件,能够给出提醒版本)

而 cargo-edit 能够主动帮忙增加依赖, 且自动更新版本号

cargo-edit 是一个很好用的工具,扩大了 Cargo 的性能,容许通过命令行批改 Cargo.toml 文件来增加、移除和降级依赖。

cargo-edit 包含几个子命令,如cargo upgrade、cargo add 和 cargo rm 等

其中,cargo addcargo rm曾经在最新的 cargo 中集成。

cargo upgrade用于将 Cargo.toml 中的依赖降级到其最新版本,并提供了不同的降级选项。

cargo set-version用于在 Cargo.toml 中设置版本号,能够通过指定版本号或主动减少主版本、次版本或修订版原本应用。

如果没有装置 cargo-edit,能够通过cargo install cargo-edit 进行装置

如果有如下报错:

error: linking with `cc` failed: exit status: 1
 |
  = note: env -u IPHONEOS_DEPLOYMENT_TARGET -u TVOS_DEPLOYMENT_TARGET LC_ALL="C" PATH="xxxxxx(一大堆 PATH 门路)"
  = note: Undefined symbols for architecture arm64:
            "_iconv", referenced from:
                _git_fs_path_iconv in liblibgit2_sys-d72cdefc619e6458.rlib(fs_path.o)
               (maybe you meant: _git_fs_path_iconv_clear, _git_fs_path_iconv_init_precompose , _git_fs_path_iconv)
            "_iconv_close", referenced from:
                _git_fs_path_direach in liblibgit2_sys-d72cdefc619e6458.rlib(fs_path.o)
                _git_fs_path_iconv_clear in liblibgit2_sys-d72cdefc619e6458.rlib(fs_path.o)
                _git_fs_path_diriter_free in liblibgit2_sys-d72cdefc619e6458.rlib(fs_path.o)
            "_iconv_open", referenced from:
                _git_fs_path_direach in liblibgit2_sys-d72cdefc619e6458.rlib(fs_path.o)
                _git_fs_path_iconv_init_precompose in liblibgit2_sys-d72cdefc619e6458.rlib(fs_path.o)
                _git_fs_path_diriter_init in liblibgit2_sys-d72cdefc619e6458.rlib(fs_path.o)
          ld: symbol(s) not found for architecture arm64
          clang: error: linker command failed with exit code 1 (use -v to see invocation)
          

error: could not compile `cargo-edit` (bin "cargo-upgrade") due to previous error
error: failed to compile `cargo-edit v0.12.2`, intermediate artifacts can be found at `/var/folders/9t/839s3jmj73bcgyp5x_xh3gw00000gn/T/cargo-installhdtjjF`.
To reuse those artifacts with a future compilation, set the environment variable `CARGO_TARGET_DIR` to that path.

可应该是 arm64 上特有的问题,可依照 cc failed: exit code: 1″>error: linking with cc failed: exit code: 1 来操作,

即在~/.cargo/config 中(如果没有此文件,则创立), 减少如下内容:

[target.x86_64-apple-darwin]
rustflags = [
  "-C", "link-arg=-undefined",
  "-C", "link-arg=dynamic_lookup",
]

[target.aarch64-apple-darwin]
rustflags = [
  "-C", "link-arg=-undefined",
  "-C", "link-arg=dynamic_lookup",
]

再次cargo install cargo-edit, 胜利装置

应用

以增加某个 crate 为例:

cargo add 须要的库名

再如

退出移动版