How do I use external crates in Rust?

Since Rust 1.0, 99% of all users will use Cargo to manage the dependencies of a project. The TL;DR of the documentation is:

  1. Create a project using cargo new

  2. Edit the generated Cargo.toml file to add dependencies:

    [dependencies]
    old-http = "0.1.0-pre"
    
  3. Access the crate in your code:

    Rust 2021 and 2018

    use old_http::SomeType;
    

    Rust 2015

    extern crate old_http;
    use old_http::SomeType;
    
  4. Build the project with cargo build

Cargo will take care of managing the versions, building the dependencies when needed, and passing the correct arguments to the compiler to link together all of the dependencies.

Read The Rust Programming Language for further details on getting started with Cargo. Specifying Dependencies in the Cargo book has details about what kinds of dependencies you can add.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)