In JavaScript and different languages, we name a stunning or inconsistent conduct a “Wat!” [that is, a “What!?”]. For instance, in JavaScript, an empty array plus an empty array produces an empty string, [] + [] === ""
. Wat!
On the different excessive, a language generally behaves with stunning consistency. I’m calling {that a} “Wat Not”.
Rust is mostly (a lot) extra constant than JavaScript. Some Rust-related codecs, nonetheless, supply surprises. Particularly, this text seems at 9 wats and wat nots in Cargo.toml
.
Recall that Cargo.toml
is the manifest file that defines your Rust challenge’s configuration and dependencies. Its format, TOML (Tom’s Apparent, Minimal Language), represents nested key/worth pairs and/or arrays. JSON and YAML are related codecs. Like YAML, however not like JSON, Tom designed TOML for straightforward studying and writing by people.
This journey of 9 wats and wat nots is not going to be as entertaining as JavaScript’s quirks (thank goodness). Nevertheless, in the event you’ve ever discovered Cargo.toml
‘s format complicated, I hope this text will show you how to really feel higher about your self. Additionally, and most significantly, whenever you be taught the 9 wats and wat nots, I hope it is possible for you to to write down your Cargo.toml
extra simply and successfully.
This text is just not about “fixing” Cargo.toml
. The file format is nice at its primary objective: specifying the configuration and dependencies of a Rust challenge. As an alternative, the article is about understanding the format and its quirks.
You most likely know tips on how to add a [dependencies]
part to your Cargo.toml
. Such a piece specifies launch dependencies, for instance:
[dependencies]
serde = "1.0"
Alongside the identical strains, you may specify improvement dependencies with a [dev-dependencies]
part and construct dependencies with a [build-dependencies]
part.
You may additionally have to set compiler choices, for instance, an optimization degree and whether or not to incorporate debugging data. You try this with profile sections for launch, improvement, and construct. Are you able to guess the names of those three sections? Is it [profile]
, [dev-profile]
and [build-profile]
?
No! It’s [profile.release]
, [profile.dev]
, and [profile.build]
. Wat?
Would [dev-profile]
be higher than [profile.dev]
? Would [dependencies.dev]
be higher than [dev-dependencies]
?
I personally favor the names with dots. (In “Wat Not 9”, we’ll see the ability of dots.) I’m, nonetheless, keen to simply bear in mind the dependences work a method and profiles work one other.
You would possibly argue that dots are wonderful for profiles, however hyphens are higher for dependencies as a result of [dev-dependencies]
inherits from [dependencies]
. In different phrases, the dependencies in [dependencies]
are additionally obtainable in [dev-dependencies]
. So, does this imply that [build-dependencies]
inherits from [dependencies]
?
No! [build-dependencies]
doesn’t inherit from [dependencies]
. Wat?
I discover this Cargo.toml
conduct handy however complicated.
You probably know that as an alternative of this:
[dependencies]
serde = { model = "1.0" }
you may write this:
[dependencies]
serde = "1.0"
What’s the precept right here? How basically TOML do you designate one key because the default key?
You possibly can’t! Normal TOML has no default keys. Wat?
Cargo TOML does particular processing on the model
key within the [dependencies]
part. It is a Cargo-specific characteristic, not a common TOML characteristic. So far as I do know, Cargo TOML gives no different default keys.
With Cargo.toml
[features]
you may create variations of your challenge that differ of their dependences. These dependences might themselves differ of their options, which we’ll name sub-features.
Right here we create two variations of our challenge. The default model is dependent upon getrandom
with default options. The wasm
model is dependent upon getrandom
with the js
sub-feature:
[features]
default = []
wasm = ["getrandom-js"][dependencies]
rand = { model = "0.8" }
getrandom = { model = "0.2", non-compulsory = true }
[dependencies.getrandom-js]
package deal = "getrandom"
model = "0.2"
non-compulsory = true
options = ["js"]
On this instance, wasm
is a characteristic of our challenge that is dependent upon dependency alias getrandom-rs
which represents the model of the getrandom
crate with the js
sub-feature.
So, how can we give this identical specification whereas avoiding the wordy [dependencies.getrandom-js]
part?
In [features],
change getrandom-js"
with "getrandom/js"
. We are able to simply write:
[features]
default = []
wasm = ["getrandom/js"][dependencies]
rand = { model = "0.8" }
getrandom = { model = "0.2", non-compulsory = true }
Wat!
Normally, in Cargo.toml
, a characteristic specification resembling wasm = ["getrandom/js"]
can listing
- different options
- dependency aliases
- dependencies
- a number of dependency “slash” a sub-feature
This isn’t normal TOML. Relatively, it’s a Cargo.toml
-specific shorthand.
Bonus: Guess the way you’d use the shorthand to say that your wasm
characteristic ought to embrace getrandom
with two sub-features: js
and test-in-browser
?
Reply: Checklist the dependency twice.
wasm = ["getrandom/js","getrandom/test-in-browser"]
We’ve seen tips on how to specify dependencies for launch, debug, and construct.
[dependencies]
#...
[dev-dependencies]
#...
[build-dependencies]
#...
We’ve seen tips on how to specify dependencies for numerous options:
[features]
default = []
wasm = ["getrandom/js"]
How would you guess we specify dependences for numerous targets (e.g. a model of Linux, Home windows, and so forth.)?
We prefix [dependences]
with goal.TARGET_EXPRESSION
, for instance:
[target.x86_64-pc-windows-msvc.dependencies]
winapi = { model = "0.3.9", options = ["winuser"] }
Which, by the principles of common TOML means we are able to additionally say:
[target]
x86_64-pc-windows-msvc.dependencies={winapi = { model = "0.3.9", options = ["winuser"] }}
Wat!
I discover this prefix syntax unusual, however I can’t counsel a greater various. I do, nonetheless, marvel why options couldn’t have been deal with the identical method:
# not allowed
[feature.wasm.dependencies]
getrandom = { model = "0.2", options=["js"]}
That is our first “Wat Not”, that’s, it’s one thing that stunned me with its consistency.
As an alternative of a concrete goal resembling x86_64-pc-windows-msvc
, chances are you’ll as an alternative use a cfg
expression in single quotes. For instance,
[target.'cfg(all(windows, target_arch = "x86_64"))'.dependencies]
I don’t take into account this a “wat!”. I believe it’s nice.
Recall that cfg
, brief for “configuration”, is the Rust mechanism normally used to conditionally compile code. For instance, in our primary.rs
, we are able to say:
if cfg!(target_os = "linux") {
println!("That is Linux!");
}
In Cargo.toml
, in goal expressions, just about the entire cfg
mini-language is supported.
all(), any(), not()
target_arch
target_feature
target_os
target_family
target_env
target_abi
target_endian
target_pointer_width
target_vendor
target_has_atomic
unix
home windows
The one elements of the cfg
mini-language not supported are (I believe) that you could’t set a price with the --cfg
command line argument. Additionally, some cfg values resembling check
don’t make sense.
Recall from Wat 1 that you just set compiler choices with [profile.release]
, [profile.dev]
, and [profile.build]
. For instance:
[profile.dev]
opt-level = 0
Guess the way you set compiler choices for a selected goal, resembling Home windows? Is it this?
[target.'cfg(windows)'.profile.dev]
opt-level = 0
No. As an alternative, you create a brand new file named .cargo/config.toml
and add this:
[target.'cfg(windows)']
rustflags = ["-C", "opt-level=0"]
Wat!
Normally, Cargo.toml
solely helps goal.TARGET_EXPRESSION
because the prefix of dependency part. You might not prefix a profile part. In .cargo/config.toml
, nonetheless, you’ll have [target.TARGET_EXPRESSION]
sections. In these sections, chances are you’ll set surroundings variables that set compiler choices.
Cargo.toml
helps two syntaxes for lists:
This instance makes use of each:
[package]
title = "cargo-wat"
model = "0.1.0"
version = "2021"[dependencies]
rand = { model = "0.8" }
# Inline array 'options'
getrandom = { model = "0.2", options = ["std", "test-in-browser"] }
# Desk array 'bin'
[[bin]]
title = "instance"
path = "src/bin/instance.rs"
[[bin]]
title = "one other"
path = "src/bin/one other.rs"
Can we alter the desk array to an inline array? Sure!
# Inline array 'bin'
bins = [
{ name = "example", path = "src/bin/example.rs" },
{ name = "another", path = "src/bin/another.rs" },
][package]
title = "cargo-wat"
model = "0.1.0"
version = "2021"
[dependencies]
rand = { model = "0.8" }
# Inline array 'options'
getrandom = { model = "0.2", options = ["std", "test-in-browser"] }
Can we alter the inline array of options right into a desk array?
No. Inline arrays of straightforward values (right here, strings) can’t be represented as desk arrays. Nevertheless, I take into account this a “wat not”, not a “wat!” as a result of it is a limitation of common TOML, not simply of Cargo.toml
.
Apart: YAML format, like TOML format, gives two listing syntaxes. Nevertheless, each of YAMLs two syntaxes work with easy values.
Here’s a typical Cargo.toml
. It mixes part syntax, resembling [dependences]
with inline syntax resembling getrandom = {model = "0.2", options = ["std", "test-in-browser"]}.
[package]
title = "cargo-wat"
model = "0.1.0"
version = "2021"[dependencies]
rand = "0.8"
getrandom = { model = "0.2", options = ["std", "test-in-browser"] }
[target.x86_64-pc-windows-msvc.dependencies]
winapi = { model = "0.3.9", options = ["winuser"] }
[[bin]]
title = "instance"
path = "src/bin/instance.rs"
[[bin]]
title = "one other"
path = "src/bin/one other.rs"
Can we re-write it to be 100% inline? Sure.
package deal = { title = "cargo-wat", model = "0.1.0", version = "2021" }dependencies = { rand = "0.8", getrandom = { model = "0.2", options = [
"std",
"test-in-browser",
] } }
goal = { 'cfg(target_os = "home windows")'.dependencies = { winapi = { model = "0.3.9", options = [
"winuser",
] } } }
bins = [
{ name = "example", path = "src/bin/example.rs" },
{ name = "another", path = "src/bin/another.rs" },
]
We are able to additionally re-write it with most sections:
[package]
title = "cargo-wat"
model = "0.1.0"
version = "2021"[dependencies.rand]
model = "0.8"
[dependencies.getrandom]
model = "0.2"
options = ["std", "test-in-browser"]
[target.x86_64-pc-windows-msvc.dependencies.winapi]
model = "0.3.9"
options = ["winuser"]
[[bin]]
title = "instance"
path = "src/bin/instance.rs"
[[bin]]
title = "one other"
path = "src/bin/one other.rs"
Lastly, let’s speak about dots. In TOML, dots are used to separate keys in nested tables. For instance, a.b.c
is a key c
in a desk b
in a desk a
. Can we re-write our instance with “numerous dots”? Sure:
package deal.title = "cargo-wat"
package deal.model = "0.1.0"
package deal.version = "2021"
dependencies.rand = "0.8"
dependencies.getrandom.model = "0.2"
dependencies.getrandom.options = ["std", "test-in-browser"]
goal.x86_64-pc-windows-msvc.dependencies.winapi.model = "0.3.9"
goal.x86_64-pc-windows-msvc.dependencies.winapi.options = ["winuser"]
bins = [
{ name = "example", path = "src/bin/example.rs" },
{ name = "another", path = "src/bin/another.rs" },
]
I respect TOML’s flexibility with respect to sections, inlining, and dots. I depend that flexibility as a “wat not”. You might discover all the alternatives it gives complicated. I, nonetheless, like that Cargo.toml
lets us use TOML’s full energy.
Cargo.toml
is a necessary software within the Rust ecosystem, providing a steadiness of simplicity and suppleness that caters to each inexperienced persons and seasoned builders. By the 9 wats and wat nots we’ve explored, we’ve seen how this configuration file can generally shock with its idiosyncrasies and but impress with its consistency and energy.
Understanding these quirks can prevent from potential frustrations and allow you to leverage Cargo.toml
to its fullest. From managing dependencies and profiles to dealing with target-specific configurations and options, the insights gained right here will show you how to write extra environment friendly and efficient Cargo.toml
information.
In essence, whereas Cargo.toml
might have its peculiarities, these traits are sometimes rooted in sensible design selections that prioritize performance and readability. Embrace these quirks, and also you’ll discover that Cargo.toml
not solely meets your challenge’s wants but additionally enhances your Rust improvement expertise.
Please comply with Carl on Medium. I write on scientific programming in Rust and Python, machine studying, and statistics. I have a tendency to write down about one article per 30 days.