Skip to content

Commit 0c3e031

Browse files
author
Chris Cho
authored
DOCSP-12465 rust connection examples (#675)
* DOCSP-12465: update Rust connection examples
1 parent 16b64cc commit 0c3e031

File tree

1 file changed

+66
-21
lines changed

1 file changed

+66
-21
lines changed

source/rust.txt

Lines changed: 66 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,35 +37,80 @@ See `Installation <https://github.com/mongodb/mongo-rust-driver#Installation>`__
3737
Connect to MongoDB Atlas
3838
------------------------
3939

40+
Select from the :guilabel:`Sync` or :guilabel:`Async` tabs below for
41+
corresponding connection code samples.
42+
4043
.. include:: /includes/atlas-connect-blurb.rst
4144

42-
.. code-block:: rust
45+
.. tabs::
46+
47+
.. tab:: Async
48+
:tabid: rust-async
49+
50+
The default async runtime used by the driver is ``tokio``. To use a
51+
different runtime, see
52+
`Configuring the async runtime <https://github.com/mongodb/mongo-rust-driver#configuring-the-async-runtime>`__.
53+
54+
.. code-block:: rust
55+
56+
use mongodb::{bson::doc, options::ClientOptions, Client};
57+
58+
#[tokio::main]
59+
async fn main() -> mongodb::error::Result<()> {
60+
// Parse your connection string into an options struct
61+
let mut client_options =
62+
ClientOptions::parse("mongodb+srv://<username>:<password>@<cluster-url>/test?w=majority")
63+
.await?;
64+
65+
// Manually set an option
66+
client_options.app_name = Some("Rust Demo".to_string());
67+
68+
// Get a handle to the cluster
69+
let client = Client::with_options(client_options)?;
70+
71+
// Ping the server to see if you can connect to the cluster
72+
client
73+
.database("admin")
74+
.run_command(doc! {"ping": 1}, None)
75+
.await?;
76+
println!("Connected successfully.");
77+
78+
// List the names of the databases in that cluster
79+
for db_name in client.list_database_names(None, None).await? {
80+
println!("{}", db_name);
81+
}
82+
Ok(())
83+
}
84+
85+
.. tab:: Sync
86+
:tabid: rust-sync
4387

44-
use bson::doc;
45-
use mongodb::{options::ClientOptions, Client};
88+
Make sure you enabled the sync API. See
89+
`Enabling the sync API <https://github.com/mongodb/mongo-rust-driver#enabling-the-sync-api>`__
90+
for more details.
4691

47-
fn main() -> mongodb::error::Result<()> {
48-
// Parse a connection string into an options struct.
49-
let mut client_options =
50-
ClientOptions::parse("mongodb+srv://<username>:<password>@<cluster-url>/test?w=majority")?;
92+
.. code-block:: rust
5193

52-
// Manually set an option.
53-
client_options.app_name = Some("Rust Demo".to_string());
94+
use mongodb::{bson::doc, sync::Client};
5495

55-
// Get a handle to the deployment.
56-
let client = Client::with_options(client_options)?;
96+
fn main() -> mongodb::error::Result<()> {
97+
// Get a handle to the cluster
98+
let client = Client::with_uri_str(
99+
"mongodb+srv://<username>:<password>@<cluster-url>/test?w=majority",
100+
)?;
57101

58-
client
59-
.database("admin")
60-
.run_command(doc! {"ping": 1}, None)?;
61-
println!("Connected successfully.");
102+
// Ping the server to see if you can connect to the cluster
103+
client
104+
.database("admin")
105+
.run_command(doc! {"ping": 1}, None)?;
106+
println!("Connected successfully.");
62107

63-
// List the names of the databases in that deployment.
64-
for db_name in client.list_database_names(None)? {
65-
println!("{}", db_name);
66-
}
67-
Ok(())
68-
}
108+
// List the names of the databases in that cluster
109+
for db_name in client.list_database_names(None, None)? {
110+
println!("{}", db_name);
111+
}
112+
Ok(())
113+
}
69114

70115
Compatibility
71116
-------------

0 commit comments

Comments
 (0)