给予Rust中结果类型的错误

ep6jt1vc  于 2023-03-18  发布在  其他
关注(0)|答案(1)|浏览(170)

我正在实现一个解析sql表的方法,它返回一个值的集合,我怎么才能有Result<StructData, Error>的返回类型呢?
在我编写的代码中,我只能返回值(StructDATA)而不返回错误。我做错了什么?

use clap::ErrorKind;
use libc::printf;
use libsqlite3_sys::Error;
use log::error;
use rusqlite::{Connection, Result};
use serde_xml_rs::expect;
use std::collections::*;
use std::error;
use std::f32::consts::E;
use std::io::{self, Read};

#[derive(Debug)]
struct Com_TO {
    id_type: i32,
    id_variable: i32,
    type_com: String,
    label_com: String,
}

pub struct Coms_TO {
    Com_TO_vec: Vec<Com_TO>, 
}

impl Coms_TO {
    pub fn new() -> Result<(Coms_TO, rusqlite::Error)> {
        let path = "XXX.db";
        let conn = Connection::open(path).expect("No possible connection");
        let mut query = conn
            .prepare("SELECT * FROM Coms_TO LIMIT 10")
            .expect("error");
        let id_iter = query
            .query_map([], |row| {
                Ok(Com_TO {
                    Id_type: row.get(0).expect("error Id_type"),
                    id_variable: row.get(1).expect("error Id_variable"),
                    type_com: row.get(2).expect("error type_com"),
                    label_com: row.get(3).expect("error label_com"),
                })
            })
            .expect("Mapping db failed");

        let id_iter_collection = id_iter.collect::<Result<Vec<Com_TO>>>();
        let id_iter_result = match id_iter_collection {
            Ok(id_iter_collection) => Coms_TO {
                Com_TO_vec: id_iter_collection,
            },
            Err(e) => panic!("{}", e),
        };
         Ok(id_iter_result)
    }
}

我尝试删除Ok(id_iter_result);以给予匹配中存在的类型,但在本例中,我获得了一个空值的不匹配,如下所示:

expected enum `Result<(Coms_TO, rusqlite::Error), rusqlite::Error>`
found unit type `()`
qij5mzcb

qij5mzcb1#

是否将new func的结果类型更改为Result<Coms_TO, rusqlite::Error>?在您的示例中,您将返回元组。

相关问题