【发布时间】:2023-03-11 17:59:01
【问题描述】:
我有以下 MWE:
use rusqlite::{Connection, Transaction };
fn main() {
let mut cn: Connection = Connection::open_in_memory().unwrap();
let tx: Transaction = cn.transaction().unwrap();
let mut stmt = tx.prepare("
INSERT INTO mytable (data) VALUES(1)
").unwrap();
stmt.execute([]).unwrap();
tx.commit().unwrap();
}
在我的 IDE(vscode)中,它显示:
tx.prepare 显示错误
在此处借用
tx
tx.commit(); 显示错误
不能搬出
tx,因为它是借来的
签名:
pub fn prepare(&self, sql: &str) -> Result<Statement<'_>> {
self.db.borrow_mut().prepare(self, sql)
}
...
#[inline]
pub fn commit(mut self) -> Result<()> {
self.commit_()
}
我不明白为什么会显示这些错误。
-
tx.prepare借用后,Transaction创建并分配给tx的所有权是什么? -
tx仍然是Transaction的所有者吗? - 当
tx.commit()被调用时,Transaction应该归tx所有,并且在调用.commit()时可以安全移动,不是吗?
此外,确定tx.prepare 的范围可以修复这些错误
use rusqlite::{Connection, Transaction };
fn main() {
let mut cn: Connection = Connection::open_in_memory().unwrap();
let tx: Transaction = cn.transaction().unwrap();
{
let mut stmt = tx.prepare("
INSERT INTO mytable (data) VALUES(1)
").unwrap();
stmt.execute([]).unwrap();
}
tx.commit().unwrap();
}
谢谢!
【问题讨论】: