【Rust】Rustのtry & catch

#![feature(catch_expr)]

use std::fs::File;
use std::io::{self, BufReader, Read};

fn main(){
    try {
        let f = File::open("foo.txt")?;
        let mut f = BufReader::new(f);
        let mut buf = String::new();
        f.read_to_string(&mut buf)?;
        println!("{}", buf);
        Ok(())
    }.unwrap_or_else(|err: io::Error| {
        eprintln!("An error occured: {}", err);
    })
}

try & catchではなく、matchで実装する

fn main(){
    let file_path = "foo.txt";
    match std::fs::remove_file(file_path) {
        Ok(()) => {},
        Err(e) => {
            eprintln!("Failed deleting file{}. Error caut:", file_path);
            // return Err(e);
        }
    }
}

$ ./main
Failed deleting filefoo.txt. Error caut:

true or falseの場合はifで大丈夫

fn main(){
    let private_key: SigningKey = SigningKey::from_bytes(&hex!(
        "DCDFF4B7CA287CC7BD30ECAEF0622265DB4E14054E12954225457C3A6B84F135"
    ).into()).unwrap();

    let public_key: &VerifyingKey = private_key.verifying_key();

    let message = b"hello, world";
    let signature: Signature = private_key.sign(message);

    let verified = public_key.verify(message, &signature).is_ok();

    println!("private key: {:X?}", hex(&private_key.to_bytes()));
    println!("public key: {:X?}", hex(&public_key.to_encoded_point(false).to_bytes()));
    println!("Signature: {:X?}", hex(&signature.to_bytes()));

    if verified {
            println!("文章は改竄されていません。");
        } else {
            println!("文章が改竄されています。");
        }

}

private key: “dcdff4b7ca287cc7bd30ecaef0622265db4e1454e12954225457c3a6b84f135”
public key: “4bac6cb0f4ad6397752c3d73b88c5c86e3d88ac695118494a1732e2abd16c76acad3d6586c37c8db7e69c2f812f99275198936957d72c38d71981991123”
Signature: “f690afffd24623627bfa97996873a6a9d8ca8e9bb7ab6ad269c929453b42985573cdc4c05b8522224065757fef6c181c8418ffd3debdab4be22a73b2e5b6ce”
文章は改竄されていません。