cargo and crates.io
Customizing builds with release profiles
Cargo has two main profiles:
- the
dev
profile- used when running
cargo build
- used when running
- the
release
profile- used when running
cargo build --release
- used when running
Full list of configuration options and defaults for each profile: https://doc.rust-lang.org/cargo/reference/profiles.html
Publishing a crate to crates.io
Documentation comments use three slashes ///
instead of two and
support markdown notation for formatting the text.
cargo doc --open
will build the HTML of your current crate’s
documentation.
Commonly used sections
- panics: scenarios in which the function being documented could panic
- errors: if the function returns a
Result
, describing the kinds of errors that might occur and what conditions might cause those errors to be returned can be helpful to callers so they can write code to handle the different kinds of errors in different ways - safety: if the function is
unsafe
to call, there should be a section explaining why the function is unsafe and covering the invariants that the function expects callers to uphold.
Documentation comments as tests
Adding example code blocks in your documentation comments can help
demonstrate how to use your library. cargo test
will run the code
examples in your documentation as tests!
Commenting contained items
Another styl of doc comment, //!
adds documentation to the item
that contains the comments rather than adding documentation to the
items following the comments. Typically used inside the crate root
file (src/lib.rs
by convention) or inside a module to document
the crate or the module as a whole.
Documentation comments within items are useful for describing crates and modules especially. Use them to explain the overall purpose of the container to help your users understand the crate’s organization.
Exporting a convenient public API with pub
use
Setting up a crates.io accounts
To create an account, visite https://crates.io and link to a Github account.
Then login with:
Adding metadata to a new crate
You can add metadata in the [package]
section in Cargo.toml
file.
Publishing in crates.io
When you’ve made changes to your crate and are ready to release a new version, you change the version value specified in your Cargo.toml file and republish. Use the Semantic Versioning rules to decide what an appropriate next version number is based on the kinds of changes you’ve made. Then run cargo publish to upload the new version.
Cargo workspaces
A workspace is a set of packages that share the same Cargo.lock
and output directory.
Then we can use the add_one
function from add-one
crate in the
adder
crate:
Adding an external dependency
To add an external dependency on add-one
, just add to the
add-one/Cargo.toml
:
Building at root level will fetch the rand
crate and the
top-level Cargo.lock
will contain information about the
dependency of add-one
on rand
.
However, it won’t be usable on adder
even though adder
has
dependency on add-one
, i.e. the dependency is not transitive.
In order to add rand
to adder
, you also need to add the
dependency on adder/Cargo.toml
.
Tests in workspaces
Installing binaries from crate.io with cargo install
Extending cargo with custom commands
Cargo is designed so you can extend it with new subcommands without having to modify Cargo. If a binary in your $PATH is named cargo-something, you can run it as if it was a Cargo subcommand by running cargo something. Custom commands like this are also listed when you run cargo —list. Being able to use cargo install to install extensions and then run them just like the built-in Cargo tools is a super convenient benefit of Cargo’s design!