expression
rusqlite: isolate an optional change with a savepoint
A savepoint is a named boundary inside a transaction. You can discard an optional group of changes without discarding earlier work. Releasing a savepoint does not make the outer transaction permanent; the outer commit still determines persistence. Run in the accompanying Cargo project. Expected stdout (not executed): kept: Pen Common mistake: Assuming savepoint rollback consumes the guard; it remains active until released or dropped.
Formal Syntax & Structure
rust syntaxrust
let sp = tx.savepoint()?; sp.commit()?;Working Example
Example Snippetrust
fn main() -> Result<(), Box<dyn std::error::Error>> { let mut db = rusqlite::Connection::open_in_memory()?; db.execute("CREATE TABLE item (name TEXT)", [])?; let mut tx = db.transaction()?; tx.execute("INSERT INTO item VALUES ('Pen')", [])?; { let mut sp = tx.savepoint()?; sp.execute("INSERT INTO item VALUES ('Book')", [])?; sp.rollback()?; sp.commit()?; } tx.commit()?; let name: String = db.query_row("SELECT name FROM item", [], |r| r.get(0))?; println!("kept: {name}"); Ok(())}