介绍


大概因为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 需要的库名


再如


好文收藏
38 声望6 粉丝

好文收集