How to handle an optional value returned by a query using the postgres crate?

前端 未结 1 352
有刺的猬
有刺的猬 2020-12-12 02:12

I\'m trying to get a value for a query, but this value can be NULL and I don\'t know how to handle it in Rust. Here is my code:

let stmt = conn.prepare(\"SEL         


        
相关标签:
1条回答
  • 2020-12-12 03:07

    As described in the documentation:

    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.

    Request an Option<Type> for the field that can be NULL; then the library will automatically convert NULL to None:

    let rating: Option<String> = row.get("rating");
    
    0 讨论(0)
提交回复
热议问题