I wrote a piece of Rust code that uses crate calamine, which looks like this
use calamine::{open_workbook, Range, Reader, Xlsx, DataType};
use std::error::Error;
#[derive(Debug)]
struct Record {
wavelength: f64,
global_tilt: f64,
}
fn main() -> Result<(), Box<dyn Error>> {
let path = format!("{}/data/am15.xlsx", std::env!("CARGO_MANIFEST_DIR"));
let mut workbook: Xlsx<_> = open_workbook(&path)?;
if let Some(Ok(range)) = workbook.worksheet_range("Sheet1").ok() {
let mut rows_read = 0;
// Iterate over the rows in the range
for row in range.rows() {
if rows_read >= 10 {
break;
}
// Parse the row into a Record struct
let record = Record {
wavelength: row.get(0).and_then(|cell| cell.get_float()).unwrap_or(0.0),
global_tilt: row.get(1).and_then(|cell| cell.get_float()).unwrap_or(0.0),
};
// Print the record
println!("Wavelength: {}, Global tilt: {}", record.wavelength, record.global_tilt);
rows_read += 1;
}
} else {
println!("Worksheet 'Sheet1' not found or could not be read.");
}
Ok(())
}
But the compilation always reports the following error:
error[E0308]: mismatched types
--> src/print_header.rs:14:15
|
14 | if let Ok(Ok(range)) = workbook.worksheet_range("Sheet1") {
| ^^^^^^^^^ ---------------------------------- this expression has type `Result<calamine::Range<Data>, XlsxError>`
| |
| expected `Range<Data>`, found `Result<_, _>`
|
= note: expected struct `calamine::Range<Data>`
found enum `Result<_, _>`
The error message I provided seems to be from a Rust compiler (rustc) indicating a type mismatch issue in my code. It looks like I am trying to match against a nested Result type instead of the expected Range type. So how can I fix this error?
New contributor
LIU MING is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.