blackbeam / rust-mysql-simple

Mysql client library implemented in rust.
Apache License 2.0
663 stars 144 forks source link

cannot use the `?` operator in a function that returns `()` #296

Closed zzke closed 2 years ago

zzke commented 2 years ago

Im trying to compile the example given in the README and it raise some errors as follows

system: macOS Big Sur rust version: 1.55.0

error[E0277]: the ? operator can only be used in a function that returns Result or Option (or another type that implements FromResidual) --> src/main.rs:13:35 11 / fn main() { 12 let url = "mysql://root:password@localhost:3307/db_name"; 13 let opts = Opts::from_url(url)?; ^ cannot use the ? operator in a function that returns () 14 let pool = Pool::new(opts)?; ... 58 println!("Yay!"); 59 } _- this function should return Result or Option to accept ?
= help: the trait `FromResidual<Result<Infallible, mysql::UrlError>>` is not implemented for `()`

note: required by from_residual

error[E0277]: the ? operator can only be used in a function that returns Result or Option (or another type that implements FromResidual) --> src/main.rs:14:31 11 / fn main() { 12 let url = "mysql://root:password@localhost:3307/db_name"; 13 let opts = Opts::from_url(url)?; 14 let pool = Pool::new(opts)?; ^ cannot use the ? operator in a function that returns () ... 58 println!("Yay!"); 59 } _- this function should return Result or Option to accept ?
= help: the trait `FromResidual<Result<Infallible, mysql::Error>>` is not implemented for `()`

note: required by from_residual

error[E0277]: the ? operator can only be used in a function that returns Result or Option (or another type that implements FromResidual) --> src/main.rs:16:35 11 / fn main() { 12 let url = "mysql://root:password@localhost:3307/db_name"; 13 let opts = Opts::from_url(url)?; 14 let pool = Pool::new(opts)?; 15 16 let mut conn = pool.get_conn()?; ^ cannot use the ? operator in a function that returns () ... 58 println!("Yay!"); 59 } _- this function should return Result or Option to accept ?
= help: the trait `FromResidual<Result<Infallible, mysql::Error>>` is not implemented for `()`

note: required by from_residual

error[E0277]: the ? operator can only be used in a function that returns Result or Option (or another type that implements FromResidual) --> src/main.rs:24:8 11 / fn main() { 12 let url = "mysql://root:password@localhost:3307/db_name"; 13 let opts = Opts::from_url(url)?; 14 let pool = Pool::new(opts)?; ... 24 )")?; ^ cannot use the ? operator in a function that returns () ... 58 println!("Yay!"); 59 } _- this function should return Result or Option to accept ?
= help: the trait `FromResidual<Result<Infallible, mysql::Error>>` is not implemented for `()`

note: required by from_residual

error[E0277]: the ? operator can only be used in a function that returns Result or Option (or another type that implements FromResidual) --> src/main.rs:43:6 11 / fn main() { 12 let url = "mysql://root:password@localhost:3307/db_name"; 13 let opts = Opts::from_url(url)?; 14 let pool = Pool::new(opts)?; ... 43 )?; ^ cannot use the ? operator in a function that returns () ... 58 println!("Yay!"); 59 } _- this function should return Result or Option to accept ?
= help: the trait `FromResidual<Result<Infallible, mysql::Error>>` is not implemented for `()`

note: required by from_residual

error[E0277]: the ? operator can only be used in a function that returns Result or Option (or another type that implements FromResidual) --> src/main.rs:52:10 11 / fn main() { 12 let url = "mysql://root:password@localhost:3307/db_name"; 13 let opts = Opts::from_url(url)?; 14 let pool = Pool::new(opts)?; ... 52 )?; ^ cannot use the ? operator in a function that returns () ... 58 println!("Yay!"); 59 } _- this function should return Result or Option to accept ?
= help: the trait `FromResidual<Result<Infallible, mysql::Error>>` is not implemented for `()`

note: required by from_residual

For more information about this error, try rustc --explain E0277. error: could not compile rust-mysql-simple due to 6 previous errors

blackbeam commented 2 years ago

Hi.

README.md assumes that a reader is somewhat familiar with basic aspects of Rust. Please read the Error Handling section of the Rust Book (especially A Shortcut for Propagating Errors: the ? Operator) as well as the Error Handling section of the Rust by Example (especially Introducing ? and Using Result in main).

You can update your main definition to fix the error (similar definition is actually used in the example):

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // ...
}

Anyway, thank for report. I'll update the README example with the fake definition of main to avoid this confusion in future.

zzke commented 2 years ago

thanks!