expression
rusqlite: decode SQL NULL with Option
SQL NULL represents an absent value. Decode a nullable text column as Option<String>; Some with an empty string is distinct from None. Request the type that matches the schema and the values actually stored. Run in the accompanying Cargo project. Expected stdout (not executed): missing: true empty: true Common mistake: Treating NULL and an empty string as interchangeable.
Formal Syntax & Structure
rust syntaxrust
row.get::<_, Option<String>>(index)?Working Example
Example Snippetrust
fn main() -> Result<(), Box<dyn std::error::Error>> { let db = rusqlite::Connection::open_in_memory()?; let missing: Option<String> = db.query_row("SELECT NULL", [], |r| r.get(0))?; let empty: Option<String> = db.query_row("SELECT ''", [], |r| r.get(0))?; println!("missing: {}", missing.is_none()); println!("empty: {}", empty.as_deref() == Some("")); Ok(())}