BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Rust 1.34 Introduces Alternative Registries for Non-Public Crates

Rust 1.34 Introduces Alternative Registries for Non-Public Crates

This item in japanese

The most significant feature in Cargo 1.34 is support for using alternative cargo registries, which could be a game changer in enterprise environments. Additionally, this release also include support for a ? operator in documentation tests, and several improvements to the standard library.

Until now, the only options for developers using proprietary/closed-source code was to specify their dependencies through either a git or path clause in their Cargo.toml files. This worked fine for most cases but had the major drawback of not supporting versioning, so you always used the latest version. With Rust 1.34, Cargo has got support for alternate registries, which are specified in .cargo/config and coexist alongside crates.io:

[registries]
my-registry = { index = "https://my-intranet:8080/git/index" }

Once you have your private registry in place, you use the registry key to declare a dependency that should be fetched from that private registry instead of cargo.io:

[dependencies]
other-crate = { version = "1.0", registry = "my-registry" }

A minimalist cargo registry can be implemented through a local git repository that contains an index and an HTTP/HTTPS server to host compressed .crate files created using cargo package. The index is made of a config.json file and one additional file for each available crate. The config.json file specifies where to get the crates available in the index and an API endpoint for publishing, if available. A package file basically contains a list of package versions available for that specific crate.

Alternate registries can support cargo publish as well. In this case, though, a simple git repository will not be enough and you will need to provide a Web API, referred through the config.json file. This API will support a number of operations, including publish (/api/v1/crates/new), yank (/api/v1/crates/{crate_name}/{version}/yank), unyank (/api/v1/crates/{crate_name}/{version}/unyank), and others. At the moment, no general reusable implementation of such an API is available.

To publish a crate to an alternate registry, you need first to save your authentication token into ~/.cargo/credentials using the cargo login command, the you can use cargo publish --registry=....

On the language level, as mentioned, Rust now supports the use of ? inside documentation tests. Documentation tests are a useful Rust feature that makes any examples you include in your documentation be used as a sort of unit test by rustdoc. Until now, documentation tests could not use ? unless they included fn main().

More changes went into the standard library. The TryFrom and TryInto conversion traits have been stabilized to add support for fallible conversions. For example, using TryFrom you can check whether a numeric conversion from an i64 to i32 is possible:

let big_number = ...i64;
let try_smaller_number = i32::try_from(big_number);
assert!(try_smaller_number.is_err());

Rust 1.34 standard library contains more stabilizations that you can check out in the detailed release notes.

As to future plans for Rust, Steve Klabnik announced in a Hacker News thread that support for async/await will start appearing in the next few releases. Additionally, Rust is expected to introduce const generics, generic associated types, and specialization.

Rate this Article

Adoption
Style

BT