Browse Source

Testing if we can overwrite the try

master
Julio Biason 2 years ago
parent
commit
7462dfdc08
  1. 7
      trynoterrortest/Cargo.lock
  2. 8
      trynoterrortest/Cargo.toml
  3. 4
      trynoterrortest/README.md
  4. 37
      trynoterrortest/src/main.rs

7
trynoterrortest/Cargo.lock generated

@ -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"

8
trynoterrortest/Cargo.toml

@ -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]

4
trynoterrortest/README.md

@ -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.

37
trynoterrortest/src/main.rs

@ -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…
Cancel
Save