I need to access a Postgres database and for that I am trying the Postgres crate ( postgres - Rust). In order to understand syntax I tried the (almost) exact example in the front page of the docs, which goes like this:
use postgres::{Client, NoTls};
fn main(){
let mut client = Client::connect("host=localhost user=postgres", NoTls)?;
client.batch_execute("
CREATE TABLE person (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
data BYTEA
)
")?;
}
The problem is that won´t compile for me.
The compiler appoints an error in the '?' at the end of 'Client::connect("host=localhost user=postgres", NoTls)?' call.
The whole error message returned by the compiler is:
the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `std::ops::FromResidual`)
cannot use the `?` operator in a function that returns `()`
help: the trait `std::ops::FromResidual<std::result::Result<std::convert::Infallible, postgres::Error>>` is not implemented for `()`rustc(E0277)
main.rs(3, 1): this function should return `Result` or `Option` to accept `?`
main.rs(5, 76): cannot use the `?` operator in a function that returns `()`
But when I hover the mouse in the 'Client::connect()' function I get that it returns a Result instance, exactly what the '?' operator needed.
What have I done wrong?