Sunday, November 12, 2017

Rust: How to use the latest version of a Cargo Crate

Why Cargo is so brilliant
Cargo is a fantastic tool. From the get-go, it's gotten a lot of things right that package managers like npm and nuget still stumble over. The competitors to these tools, yarn and paket address the issues that, thankfully, Cargo has already solved.

You know what npm got right, though? It's dead simple to hook a package up. Do you want to use react? You can just type npm install --save react, and it will find the latest version of React, and add it to the packages.json file. Cargo, too, can be abused to have this functionality.

Warning: You really shouldn't do this...
By explicitly listing versions in your Cargo.toml file, you know for a fact that a future release of the package won't break your project. Furthermore, you know for a fact that the different dependencies that you're relying on play nicely together.

One of the goals of dev-ops is minimizing risk. Having an explicit definition of the packages that could break your project is one of the most important ways to do this.

With that said...
A quick spiel on Semver
Semver, the syntax for identifying the version of the package you want to use, is a widely upheld standard. You can read the full semver guidelines to grok exactly what's happening when you type a version in. Cargo use's a Rust semver library (which can be found here) to parse its dependencies version section.

By abusing the Greater-Than functionality, we can trick Cargo to installing the latest version of a package.

Example: Installing the latest Serde
This line in your Cargo.toml file:

serde = ">0.0.0"

is all you need for Cargo to install the latest version of Serde. This can be used for any package that you don't care about the version for. Be careful with this trick. There's a reason Cargo doesn't technically support this type of workflow outside of this hack.

2 comments:

  1. This way my associate Wesley Virgin's story launches with this SHOCKING AND CONTROVERSIAL video.

    Wesley was in the military-and soon after leaving-he revealed hidden, "mind control" secrets that the CIA and others used to get whatever they want.

    These are the exact same SECRETS tons of famous people (especially those who "became famous out of nothing") and the greatest business people used to become rich and successful.

    You've heard that you utilize only 10% of your brain.

    That's really because the majority of your BRAINPOWER is UNTAPPED.

    Perhaps this expression has even taken place INSIDE your own mind... as it did in my good friend Wesley Virgin's mind about 7 years ago, while riding an unregistered, garbage bucket of a car without a license and $3.20 on his banking card.

    "I'm absolutely frustrated with living payroll to payroll! Why can't I become successful?"

    You've taken part in those types of conversations, ain't it right?

    Your own success story is going to start. All you have to do is in YOURSELF.

    Take Action Now!

    ReplyDelete