1

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.
mcarton
  • 27,633
  • 5
  • 85
  • 95
yogibear
  • 320
  • 2
  • 13
  • I don't see any `connect` method being called to mongodb ? can you please check. – stud3nt Dec 31 '19 at 12:43
  • You are correct, I can't see it either. Thanks for spotting that. I will have a look and report back. – yogibear Dec 31 '19 at 13:06
  • It looks like; the following code connects: let client = Client::with_options(client_options)?; but am looking into it, tricky finding info. – yogibear Dec 31 '19 at 13:11
  • @edwardw not sure it does answer the question I have. Although I will look more into it. Maybe I should log a new request for a working code example to connect and use a mongo db in rust? – yogibear Dec 31 '19 at 13:22
  • I have re-read the supplied link that edwardw shared (thanks). I have managed to reduce the errors. I moved the mongo code into a function, and called it from main. Im now only getting one error: – yogibear Dec 31 '19 at 13:46
  • The error is: error: expected one of `(` or `<`, found `TestMongo` --> src\main.rs:10:11 | 10 | fn Result TestMongo() { | ^^^^^^^^^ expected one of `(` or `<` here error: aborting due to previous error error: could not compile `mongo`. – yogibear Dec 31 '19 at 13:46
  • I might have to revisit the general documentation on Rust and the book im reading. Thanks for the help so far. Should anyone have an answer for the last error that would be great. Regardless, im going to re-read the link edwardw shared and the book I have in more detail (not to mention more internet searching). – yogibear Dec 31 '19 at 13:46

0 Answers0