Hi I am new to Rust (started a few days ago). Just getting used to Mongo itself, mainly with Java and C#). Having got some of the basics sorted with Rust I want to incorporate Mongo DB.
I found this page from 19th Dec this year; Offical Mongo Driver
I have tried following the sample code on that page and I have the following dependancies in my Cargo.toml file.
[dependencies]
mongodb = "0.9.0"
bson = "0.14.0"
The Mongo.rs code file I have is as follows: -
use mongodb::{Client, options::ClientOptions};
fn main() {
// Parse a connection string into an options struct.
let mut client_options =
ClientOptions::parse("mongodb://localhost:27017")?;
// Manually set an option.
client_options.app_name = Some("My App".to_string());
// Get a handle to the deployment.
let client = Client::with_options(client_options)?;
// List the names of the databases in that deployment.
for db_name in client.list_database_names(None)? {
println!("{}", db_name);
}
}
When using cargo build im seeing the following errors shown below. There is a lot in Rust itself im still not understanding. Presently working through the book "The Rust Programming Language" by Steve and Carol. But often when learning new things I like to take a side tour of something else and was expecting the code sample to work.
Is anyone able to help, I would like to learn whats wrong and why on the way to getting the code working. I think it will be good way to learn more about Rust.
Thanks in advance.
Compiling mongo v0.1.0 (C:\Users\Russell\OneDrive\GIT_RUST\RustMongo)
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
--> src\main.rs:7:9
|
7 | ClientOptions::parse("mongodb://localhost:27017")?;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot use the `?` operator in a function that returns `()`
|
= help: the trait `std::ops::Try` is not implemented for `()`
= note: required by `std::ops::Try::from_error`
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
--> src\main.rs:13:18
|
13 | let client = Client::with_options(client_options)?;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot use the `?` operator in a function that returns `()`
|
= help: the trait `std::ops::Try` is not implemented for `()`
= note: required by `std::ops::Try::from_error`
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
--> src\main.rs:16:20
|
16 | for db_name in client.list_database_names(None)? {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot use the `?` operator in a function that returns `()`
|
= help: the trait `std::ops::Try` is not implemented for `()`
= note: required by `std::ops::Try::from_error`
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0277`.
error: could not compile `mongo`.
To learn more, run the command again with --verbose.
PS C:\Users\Russell\OneDrive\GIT_RUST\RustMongo> cargo build
Updating crates.io index
Compiling mongo v0.1.0 (C:\Users\Russell\OneDrive\GIT_RUST\RustMongo)
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
--> src\main.rs:7:9
|
7 | ClientOptions::parse("mongodb://localhost:27017")?;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot use the `?` operator in a function that returns `()`
|
= help: the trait `std::ops::Try` is not implemented for `()`
= note: required by `std::ops::Try::from_error`
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
--> src\main.rs:13:18
|
13 | let client = Client::with_options(client_options)?;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot use the `?` operator in a function that returns `()`
|
= help: the trait `std::ops::Try` is not implemented for `()`
= note: required by `std::ops::Try::from_error`
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
--> src\main.rs:16:20
|
16 | for db_name in client.list_database_names(None)? {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot use the `?` operator in a function that returns `()`
|
= help: the trait `std::ops::Try` is not implemented for `()`
= note: required by `std::ops::Try::from_error`
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0277`.
error: could not compile `mongo`.
To learn more, run the command again with --verbose.