0

TOML学习

 1 year ago
source link: https://jasonkayzk.github.io/2022/11/28/TOML%E5%AD%A6%E4%B9%A0/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Rust中的Cargo配置文件为TOML,这里通过一些文档简单学习了一下;

TOML学习

简而言之,用 [table] 表的形式来归并多个配置,并且去掉了 JSON 括号的形式或者 YAML 缩进的形式;

下面是一个例子:

# 单个配置
abc = "hello"

# 数组
arr = [1,2,3]

# 内联表
person = {name = { first = "Tom", last = "Preston-Werner" }}

# 表(统一的配置)
[table]
option = false
[table.nested]
option = true

# 表数组
[[table-arr]]
name = "Hammer"
sku = 738594937

[[table-arr]]  # 数组里的空表

[[table-arr]]
name = "Nail"
sku = 284758393
color = "gray"

对应的JSON:

{
  "abc": "hello",
  "arr": [1, 2, 3],
  "person": {
    "name": {
      "first": "Tom",
      "last": "Preston-Werner"
    }
  },
  "table": {
    "option": false,
    "nested": {
      "option": true
    }
  },
  "table-arr": [
    {
      "name": "Hammer",
      "sku": 738594937
    },
    {},
    {
      "name": "Nail",
      "sku": 284758393,
      "color": "gray"
    }
  ]
}

Rust中的Cargo配置

在 Rust 圣经中展示了 Cargo 的配置:

可以看到 Cargo Target 列表:(查看 Target 配置 获取详细设置)

可以看到,库的定义为表:[lib],而其他配置例如 [[bin]] 则为表数组;

这是正好对应了Rust中的规定:

一个 Crate 中只能定义一个 lib 对象,但是可以在 Cargo.toml 中创建多个 bin、example 对象;


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK