关于 Cargo.toml 的依赖项中的 toml 换行符

About toml line break in dependencies of Cargo.toml

我写了Cargo.toml的依赖:

[dependencies ]
opencv = {version = "0.26", default-features = false, features = ["opencv-41"]}

以上一行可以通过编译

我想为依赖项分开行。

本页 https://github.com/BurntSushi/toml-test/blob/master/tests/valid/multiline-string.toml

说的解决方法。

我参考这个解决方案来编写我的依赖项:

[dependencies ]
opencv = """\{version = "0.26", \
default-features = false, \
features = ["opencv-41"] \
}"""

但是编译器抛出错误:

$ cargo build                                          [±master ●]

error: failed to parse manifest at /home/curlywei/WorkSpace/rust/testcargo/Cargo.toml

Caused by:
  failed to parse the version requirement { version = "0.26", default-features = false, features = ["opencv-41"] } for dependency opencv

Caused by:
  the given version requirement is invalid

我该怎么做?

您的 link 包含多行示例 字符串 ,而不是 table。在 TOML 中,行内 table 可能不包含换行符。

相反,TOML spec 建议使用常规 table:

No newlines are allowed between the curly braces unless they are valid within a value. Even so, it is strongly discouraged to break an inline table onto multiples lines. If you find yourself gripped with this desire, it means you should be using standard tables.

[dependencies.opencv]
version = "0.26"
default-features = false
features = ["opencv-41"]