Julio Biason
2 years ago
4 changed files with 56 additions and 0 deletions
@ -0,0 +1,7 @@ |
|||||||
|
# This file is automatically @generated by Cargo. |
||||||
|
# It is not intended for manual editing. |
||||||
|
version = 3 |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "trynoterrortest" |
||||||
|
version = "0.1.0" |
@ -0,0 +1,8 @@ |
|||||||
|
[package] |
||||||
|
name = "trynoterrortest" |
||||||
|
version = "0.1.0" |
||||||
|
edition = "2021" |
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html |
||||||
|
|
||||||
|
[dependencies] |
@ -0,0 +1,4 @@ |
|||||||
|
# TryNotErrorTest |
||||||
|
|
||||||
|
Checking if the try operator (`?`) accepts capturing an error, but returning |
||||||
|
something else, which does have a conversion from the error to it. |
@ -0,0 +1,37 @@ |
|||||||
|
#[derive(Debug)] |
||||||
|
enum MyType { |
||||||
|
SomethingWrong, |
||||||
|
SomethingGood, |
||||||
|
} |
||||||
|
|
||||||
|
enum SomeError { |
||||||
|
Bad, |
||||||
|
} |
||||||
|
|
||||||
|
impl From<SomeError> for MyType { |
||||||
|
fn from(value: SomeError) -> Self { |
||||||
|
MyType::SomethingWrong |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
fn validate(s: &str) -> Result<(), SomeError> { |
||||||
|
if s == "bad" { |
||||||
|
Err(SomeError::Bad) |
||||||
|
} else { |
||||||
|
Ok(()) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
fn check(response: &str) -> MyType { |
||||||
|
let inner = validate(&response)?; |
||||||
|
println!("Inner: {:?}", inner); |
||||||
|
inner |
||||||
|
} |
||||||
|
|
||||||
|
fn main() { |
||||||
|
let results = ["good", "bad", "good"]; |
||||||
|
for variant in results { |
||||||
|
let response = check(&variant); |
||||||
|
println!("{} = {:?}", variant, response); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue