Rust:闭包组合所有权

时间:2021-08-01 16:22:34

标签: rust closures ownership

为了避免嵌套匹配,我尝试对 and_then 使用 Result 方法。当我尝试转换这个时出现问题

match Connection::open(&db_path) {
    Ok(conn) => {
        match conn.prepare("SELECT qwe,username_value,wer FROM my_table;") {
            Ok(mut stmt) => {
                match stmt.query_map([], |row| {Ok(row.get_unwrap::<usize, String>(1).clone())}) {
                    Ok(usernames) => {
                        for username in usernames {
                            if username.is_ok() {
                                println!("{}", username.expect("Could not read username value"));
                            }
                        }
                        println!("Query done");
                    },
                    Err(err) => println!("Error {}", err),
                }
            },
            Err(err) => println!("Error {}", err),
        }
    },
    Err(err) => println!("Error {}", err),
}

进入这个

match Connection::open(&db_path)
    .and_then(move |conn: Connection| conn.prepare("SELECT qwe,username_value,wer FROM my_table;"))
    .and_then(|mut stmt: Statement| stmt.query_map([], |row| {Ok(row.get_unwrap::<usize, String>(1).clone())})) {
    Ok(usernames) => {
        for username in usernames {
            if username.is_ok() {
                println!("{}", username.expect("Could not read username value"));
            }
        }
        println!("Query done");
    },
    Err(_) => println!("Error querying db"),
}

SQL 库是 rusqlite。

第一个片段被编译器接受,第二个不是。 编译器用以下错误消息向我尖叫:

error[E0515]: cannot return value referencing function parameter `conn`
   --> src\main.rs:134:43
    |
134 |         .and_then(move |conn: Connection| conn.prepare("SELECT qwe,username_value,wer FROM my_table;"))
    |                                           ----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |                                           |
    |                                           returns a value referencing data owned by the current function
    |                                           `conn` is borrowed here

error[E0515]: cannot return value referencing function parameter `stmt`
   --> src\main.rs:135:41
    |
135 |         .and_then(|mut stmt: Statement| stmt.query_map([], |row| {Ok(row.get_unwrap::<usize, String>(1).clone())})) {
    |                                         ----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |                                         |
    |                                         returns a value referencing data owned by the current function
    |                                         `stmt` is borrowed here

error: aborting due to 2 previous errors

我在三个闭包上尝试使用 move 但错误消息是相同的。 为什么第二个片段中的所有权是错误的,这是写这样的东西的惯用方式?

1 个答案:

答案 0 :(得分:1)

问题是 conn.prepare 返回一个引用 conn 的语句,但是 conn 在闭包结束时被销毁,因此当语句尝试使用它时它不再存在.做你想做的惯用方法是使用 ? 运算符:

let conn = Connection::open(&db_path)?;
let mut stmt = conn.prepare("SELECT qwe,username_value,wer FROM my_table;")?;
for username in stmt.query_map([], |row| {Ok(row.get_unwrap::<usize, String>(1).clone())})? {
    if username.is_ok() {
        println!("{}", username.expect("Could not read username value"));
    }
}

并让调用者处理错误。如果你想自己处理错误,你可以将上面的代码包装在一个本地函数或闭包中(另见this question)。