Install Rust and Cargo
Ensure you have Rust 1.74 or later, and Cargo, the Rust package manager, installed in your development environment.
For information about how to install Rust and Cargo, see the official Rust guide on downloading and installing Rust.
Create a Project Directory
In your shell, run the following command to create a
directory called rust_quickstart for this project:
cargo new rust_quickstart
When this command successfully completes, you have a Cargo.toml
file and a src directory with a main.rs file in your
rust_quickstart directory.
Run the following command to navigate into the project directory:
cd rust_quickstart
Install the Rust Driver
Add the following crates to your project by including them in the
dependencies list located in your project's Cargo.toml file:
mongodb, the Rust driver crateserde, the serialization cratefutures, an asynchronous runtime crate that provides core abstractions
Tip
The mongodb crate resolves the bson crate, which is the primary
MongoDB data representation crate. You can omit the bson
crate in your dependencies list.
The driver supports both asynchronous and synchronous runtimes. To see example dependency lists for each runtime, select from the following Asynchronous API and Synchronous API tabs:
[dependencies] serde = "1.0.188" futures = "0.3.28" tokio = {version = "1.32.0", features = ["full"]} [dependencies.mongodb] version = "3.3.0"
[dependencies] serde = "1.0.188" [dependencies.mongodb] version = "3.3.0" features = ["sync"]
To learn more about asynchronous and synchronous runtimes, see the Asynchronous and Synchronous APIs guide.
After you complete these steps, you have Rust and Cargo installed and a new Rust project with the necessary driver dependencies.