Getting started

At its heart, TOML is all about key-value pairs. This is the simplest TOML file you can create:

message = "Hello World"

Here, the key message has the "Hello World" value. A value can also be an array:

messages: ["Hello", "World", "out", "there"]

A group of key-values is called a table. The following TOML lets the smileys table contain the happy key with the ":)" value and the sad key with the ":(" value:

[smileys]
happy = ":)"
sad = ":("

A particularly small table can be inlined, that is, written in one line. The last example is the exact same as the following:

smileys = { happy = ":)", sad = ":(" }

Tables can be nested by separating their names with a dot:

[servers]
[servers.production]
ip = "192.168.0.1"
[servers.beta]
ip = "192.169.0.2"
[servers.testing]
ip = "192.169.0.3"

A nice property of TOML is that you can convert any key into a table if you need to specify additional information. For example, Cargo itself expects this when declaring dependency versions. For example, if you wanted to use rocket_contrib, a helper crate of the popular rocket web framework for Rust, at version 0.3.3, you would write this:

[dependencies]
rocket_contrib = 0.3.3

However, if you wanted to specify the exact features to be included in rocket_contrib, you would need to instead write it as a sub-table of dependencies. The following TOML would tell Cargo to use its JSON serialization feature:

[dependencies]
[dependencies.rocket_contrib]
version = "0.3.3"
default-features = false
features = ["json"]

Another nice thing TOML brings to the table is that its whitespace is not significant, that is, you can indent a file however you want. You can even add comments by beginning a line with the following:

# some comment

If you want to explore the format further, the entirety of the TOML syntax is specified at https://github.com/toml-lang/toml.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.57.164