PostgreSQL
 SQL >> Datenbank >  >> RDS >> PostgreSQL

Optionale Werte aus Abfragen mit der Rust-postgres-Crate behandeln

Wie die Dokumentation erklärt:

Nullability

In addition to the types listed above, FromSql is implemented for Option<T> where T implements FromSql. An Option<T> represents a nullable Postgres value.

Fordern Sie einfach Option<Type> für jedes Feld an, das NULL sein kann, und die Bibliothek wandelt NULL automatisch in None:

let rating: Option<String> = row.get("rating");

Wollen Sie statt eines Werts einen leeren String, ergänzen Sie unwrap_or_default() – so erhalten Sie String::new() anstelle von None.