VOOZH about

URL: https://thenewstack.io/tutorial-import-libraries-of-rust-code-with-crates-io/

⇱ Tutorial: Import Libraries of Rust Code with Crates.io - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2019-09-03 12:15:02
Tutorial: Import Libraries of Rust Code with Crates.io
tutorial,
Rust / Software Development

Tutorial: Import Libraries of Rust Code with Crates.io

How Rust programmers use the Rust "crates.io" library.
Sep 3rd, 2019 12:15pm by David Cassel
👁 Featued image for: Tutorial: Import Libraries of Rust Code with Crates.io
Feature image: Rusty the Crab, on a background image from Pixabay.

When you’re first getting started with Rust, you’ll eventually discover that there’s an all-important repository where users share their Rust code in modules — called “crates” — at crates.io. It’s Rust’s official package registry, which proudly informs visitor’s that they’ve now seen 1,397,892,571 downloads. (There’s over 29,187 to choose from.)

And fortunately, it’s really easy to import these libraries into your own Rust code…

Rust’s official “Getting Started” guide not only points visitors to the massive treasury of shared code modules at crates.io, but it also demonstrates how to install them with a package called, “Ferris Says.”

This will be my first encounter with Rust’s syntax for importing a library.

As I learned in my first week of Rust, even writing a simple “Hello World” program involves creating both the source file for the code itself plus a second manifest file. But the good news is that Rust’s tool automatically creates that manifest file — and also helpfully includes a blank section where dependencies (like imported Rust libraries) can be added later.

👁 A Hello World manifest in Rust

And each library’s page at crates.io begins with the exact line of code to add into your manifest file to import it. In fact, to add it into your manifest, you don’t even have to type out the line of code. The icon to the right of the code lets you copy it straight into your buffer.

👁 Rust library Ferris says copying to clipboard

👁 Ferris says copied to clipboard

So I add the appropriate line into my manifest file Cargo.toml — then get ready to compile and run the new code.

👁 Cargo toml - now with ferris-says librar

The “Getting Started” guide also suggests replacing my old Hello World code…

fn main() {
 println!("Hello, world!");
}

…with

use ferris_says::say; // from the previous step
use std::io::{stdout, BufWriter};

fn main() {
 let stdout = stdout();
 let out = b"Hello fellow Rustaceans!";
 let width = 24;

 let mut writer = BufWriter::new(stdout.lock());
 say(out, width, &mut writer).unwrap();
}

One cut-and-paste later, I’m ready to go. So when I type to compile the code, I’m surprised how long it takes to add what seem to be very small changes. But then I realize cargo has taken this opportunity to update its internal index of files at crates.io. Several minutes go by as the cargo program tells me it’s still updating. I watch the ASCII art arrow move slowly across my screen…

👁 Updating crates io index

But finally they’ve all been downloaded — and then compiled — so I can get back to the important business of printing out “Hello, world” with my newly-installed Rust library.

And it works!

👁 Ferris says Hello Fellow Rustaceans

“You’re a Rustacean now!” explains the Getting Started guide. “Welcome! We’re so glad to have you.”

Exploring the Rust Libraries at Crates.io

Browsing through crates.io, I eventually discover that there’s a “categories” page that groups the downloadable libraries into 47 different categories. There’s a category with 532 algorithm crates, promising Rust implementations of core algorithms like sorting and searching, and 68 date and time crates “to manage the inherent complexity of dealing with the fourth dimension.”

Of course, that’s not the only place where you can import libraries. The official documentation for cargo reminds you that a library can just as easily come from a git repository, or even from subdirectories on your local file system. (And there’s fancy tricks for changing the dependencies depending on which platform is running the code.)

In fact, a GitHub repository named “rust-unofficial” also offers a curated collection of what it calls Awesome Rust, which includes links to libraries, development tools and even the code for entire applications.

But it turns out there’s another even easier way to play with crates. The top 100 most-downloaded crates are already pre-installed in the online “Rust Playground.” And this also gives you a chance to see what a manifest file looks like when it’s importing 100 different dependencies. In addition, it also includes all the libraries from “The Rust Cookbook,” which describes itself as “a collection of simple examples that demonstrate good practices to accomplish common programming tasks, using the crates of the Rust ecosystem.”

What I like about the “Cookbook” as a learning resource is it helpfully indicates which crates you’ll need for each example — and those two-color “badges” (with the crate names) link to detailed documentation.

👁 list of crates

Plus, working my way through the “Cookbook,” I also discovered there’s a much easier way to add dependencies to a manifest.

When you’re in the project’s directory (which has the Cargo.toml file), there’s a way to just use the command-line command and the library’s name.

Though ironically, this easier way of adding new dependencies to your manifest is only enabled after — adding a new dependency to your manifest. Specifically, you need to install the tool, whose webpage (on GitHub) describes it as “a utility for managing cargo dependencies from the command line.”

can also take several minutes to install — but then your Rust environment is fully equipped with a nice simple command for adding new crates from the command line.

And if you ever change your mind about a dependency, there’s also a way to cleanly remove it from your manifest altogether. Just replace “add” with “rm” in the command.

And there’s also a third command. When you’re in a project’s directory, typing will now check each of the project’s dependencies to see if there are newer versions available for any of them — and if so, will perform the necessary updates.

These are tools are deceptively simple and incredibly powerful, giving you the flexibility to choose from tens of thousands of new code modules to add to the future Rust projects that await you.

So now the fun can really begin. Because once you know how to add one Rust library, you can start playing around with adding more!

TRENDING STORIES
David Cassel is a proud resident of the San Francisco Bay Area, where he's been covering technology news for more than two decades. Over the years his articles have appeared everywhere from CNN, MSNBC, and the Wall Street Journal Interactive...
Read more from David Cassel
SHARE THIS STORY
TRENDING STORIES
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.