Type annotation in a pattern match in Rust? -
i'm digging rust, gracefully handling errors, i'm having little trouble type inference.
extern crate mysql; use mysql my; fn main() { my_test(); } fn my_test() -> result<(), my::error> { let pool = try!(my::pool::new("")); let res = try!(pool.prep_exec("select 1 count", ())); rows in res { let row: my::row = try!(rows); match row.take("count") { none => (), some(i) => println!("{:?}", i), }; } ok(()) }
which leads to
src/bin/main.rs:86:12: 86:13 error: unable infer enough type information
_
; type annotations or generic parameter binding required [e0282]
unfortunately docs in crate use unwrap
lot, not me. in haskell, println!("{:?}", :: i32)
, can't figure out how in rust. i've tried various ways cast row.take
, i've haven't had luck. i'd love see variety of ways in have structured code, if there more idiomatic way of going it.
looking @ row::take
documentation can see 2 types parameter t
, i
. type i
inferred "count"
argument, type t
used return type. have 2 options specify return type, explicit in method call, or implicit in type of variable (like did row
):
fn my_test() -> result<(), my::error> { let pool = try!(my::pool::new("")); let res = try!(pool.prep_exec("select 1 count", ())); rows in res { let mut row: my::row = try!(rows); // specify type t explicitly, let type inferred match row.take::<i32, _>("count") { none => (), some(i) => println!("{:?}", i), }; // or let s: option<i32> = row.take("count"); } ok(()) }
the type ascription rfc proposes syntax (similar haskell example) annotating sub-expression type.
Comments
Post a Comment