如何指定自定义 Cargo 输出目录?

How can I specify a custom Cargo output directory?

我把这个放在我的 Cargo.toml

[build]
target-dir = "../my-target"

但是,Cargo 无法识别此密钥。

cargo run --release --bin my_project

warning: unused manifest key: build
error: failed to open: /.../project-root/target/releases/.cargo-lock

Caused by:
  Permission denied (os error 13)

带有环境变量的自定义目标目录有效:

CARGO_TARGET_DIR=../my-target cargo run --bin my_project

但是如何在 Cargo.toml 中指定“../my-target”?

[build]Cargo-level configuration 而不是项目:

This document will explain how Cargo’s configuration system works, as well as available keys or configuration. For configuration of a project through its manifest, see the manifest format.

把你的 [build] 放在 $PROJECT_DIR/.cargo/config 甚至 $HOME/.cargo/config 里面。所有选项见上文link。

使用CARGO_TARGET_DIR environment variable:

CARGO_TARGET_DIR=../my-target cargo run --bin my_project

(问题中有说明,但我想为跳过该问题的任何人强调)