Problem in reading `.fgb` file

Hi,
I tried to reaf a fgb file as ``` use flatgeobuf::*;
use std::fs::File;
use std::io::BufReader;

fn main() {
let mut filein = BufReader::new(File::open("countries.fgb")?);
let mut fgb = FgbReader::open(&mut filein)?.select_all()?;
while let Some(feature) = fgb.next()? {
println!("{}", feature.property::("name").unwrap());
println!("{}", feature.to_json()?);
}
}```. cargo build says:

  Compiling geo1 v0.1.0 (/usr/local/geo1)
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:6:64
 |
5 | fn main() {
 | --------- this function should return `Result` or `Option` to accept `?`
6 |     let mut filein = BufReader::new(File::open("countries.fgb")?);
 |                                                                ^ cannot use the `?` operator in a function that returns `()`
 |
 = help: the trait `FromResidual<Result<Infallible, std::io::Error>>` is not implemented for `()`

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:7:47
 |
5 | fn main() {
 | --------- this function should return `Result` or `Option` to accept `?`
6 |     let mut filein = BufReader::new(File::open("countries.fgb")?);
7 |     let mut fgb = FgbReader::open(&mut filein)?.select_all()?;
 |                                               ^ cannot use the `?` operator in a function that returns `()`
 |
 = help: the trait `FromResidual<Result<Infallible, flatgeobuf::Error>>` is not implemented for `()`

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:7:61
 |
5 | fn main() {
 | --------- this function should return `Result` or `Option` to accept `?`
6 |     let mut filein = BufReader::new(File::open("countries.fgb")?);
7 |     let mut fgb = FgbReader::open(&mut filein)?.select_all()?;
 |                                                             ^ cannot use the `?` operator in a function that returns `()`
 |
 = help: the trait `FromResidual<Result<Infallible, flatgeobuf::Error>>` is not implemented for `()`

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:8:41
 |
5 | fn main() {
 | --------- this function should return `Result` or `Option` to accept `?`
...
8 |     while let Some(feature) = fgb.next()? {
 |                                         ^ cannot use the `?` operator in a function that returns `()`
 |
 = help: the trait `FromResidual<Result<Infallible, flatgeobuf::Error>>` is not implemented for `()`

error[E0599]: no method named `to_json` found for reference `&FgbFeature` in the current scope
 --> src/main.rs:10:32
  |
10 |         println!("{}", feature.to_json()?);
  |                                ^^^^^^^ method not found in `&FgbFeature`

Some errors have detailed explanations: E0277, E0599.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `geo1` (bin "geo1") due to 5 previous errors ```

Your main function returns the unit type, (), which is the default when no return type is specified, and the error is telling you that it must return a Result, since the ? will cause a Result to be returned when there is an error.

1 Like

Just for reference, in Rust nomenclature this is called the unit type. This might be important for OP as they may want to change their main function to return a Result<(), SomeError>.

1 Like

The ? operator is the rough equivalent of "unwrap the value or return from the function", where the value returned has the same type as the unwrapped value -- e.g. return None when failing to unwrap an Option, or return Err(the_error.into()) when failing to unwrap a Result.

Something like this will probably work for you:

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Your current code
    Ok(())
}

Or changing all the ? to .unwrap() or .expect("failed to open file") or the like.

Or gracefully handling the errors and not panicking.

Here's the relevant section of the book.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.