I was playing around with try
blocks but I can't figure out how to make this code work (Playground):
#![feature(try_blocks)]
fn get_val(vec: Vec<i32>) -> Result<i32, String> {
let (v1, v2) =
try { (vec.get(0)?, vec.get(1)?) }.ok_or("failed to collect elements".to_string())?;
if v1 < v2 {
return Err("condition failed".to_string());
}
Ok(v1 + v2)
}
error[E0282]: type annotations needed
--> src/lib.rs:5:9
|
5 | try { (vec.get(0)?, vec.get(1)?) }.ok_or("failed to collect elements".to_string())?;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type
|
= note: type must be known at this point