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.


Update

For modern Rust, see this answer.


Original answer

You need to pass the -L flag to rustc to add the directory which contains the compiled http library to the search path. Something like rustc -L path-to-cloned-rust-http-repo/build your-source-file.rs should do.

Tutorial reference

Tags:

Libraries

Rust