1

How can I return my own string errors in rust?

fn main() -> std::io::Result<(), std::io::Error> {
    let a = 30;
    if a == 10 {
        println!("ten");
    } else if a == 20 {
        println!("twenty");
    } else {
        Err("Not ten or twenty".to_string())
    }
    Ok(())
}

This gives

| fn main()->std::io::Result<(),std::io::Error>{
| 

                          ^^^^^^^^^^^^^^ unexpected type argument
Ömer Erden
  • 7,680
  • 5
  • 36
  • 45
Eka
  • 14,170
  • 38
  • 128
  • 212
  • I believe panic exists the program, I dont want to do that. – Eka Feb 21 '21 at 09:21
  • I dont know about `std::eprintln!` is it similar to `print!` or `println!` macros then I would avoid it. I want to use `Err` with `Result` – Eka Feb 21 '21 at 09:25
  • The main program returns a value only once when it is terminated. – Rabbid76 Feb 21 '21 at 09:26
  • I want to use the error logic not only in the `main` function but with others as well. The above code is just an example code. `eprintln` I believe is not the rust idiomatic way to do error handling. – Eka Feb 21 '21 at 09:30
  • 4
    [Here's the linked answers applied to your problem](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=0cc62d7bd3e3cbeb39a11ac954ac5a8b). The error presented is because `io::Result<>` is a `Result` with the error type defaulted to `io::Error` already. – kmdreko Feb 21 '21 at 12:21

0 Answers0