Handling Errors

Hi im in the handling error section on the enum part, i have errors in the code while Zsolt the instructor was able to run the code, i even copied the code but still the same.
I cant find what is the problem we use the same version and the code is the same.
`
enum Result<ValueType, Error> {
Ok(ValueType),
Err(Error)
}

fn divide(a: f32, b: f32) -> Result<f32, String> {
if b == 0.0 {
return Err(“Division by zero”.to_string()); // have error here mismatched types
}
return Ok(a / b);
}

fn print_result(result: Result<f32, String>) -> () {
match result {
Ok(value) => { // error here mismatched types
println!(“Result: {}”, value);
}
Err(message) => {. // error here mismatched types
println!(“Error: {}”, message);
}
}
}

fn main() {
let num = 5.0;
let b1 = 2.0;
let b2 = 0.0;

print_result( divide(num, b1) );
print_result( divide(num, b2) );
}`

Could you please share your code in the following way?

Carlos Z

I’m having the same issue here on Error Handling - Result enum.
I’ve tried with typed code and also tried with copy/paste code and still get the same errors.

code:
// Result enum
enum Result<ValueType, Error> {
    Ok(ValueType),
    Err(Error)
}

fn divide(a: f32, b: f32) -> Result<f32, String> {
    if b == 0.0 {
      return Err("Division by zero".to_string());
    } 
    return Ok(a / b);
}

fn print_result(result: Result<f32, String>) -> () {
    match result {
        Ok(value) => { 
            println!("Result: {}", value);
        } 
        Err(message) => {
            println!("Error: {}", message);
        }
    }  
}

fn main() {
    let num = 5.0;
    let b1 = 2.0;
    let b2 = 0.0;

    print_result( divide(num, b1) ); 
    print_result( divide(num, b2) );
}
output:
error[E0308]: mismatched types
 --> src\main.rs:9:14
  |
7 | fn divide(a: f32, b: f32) -> Result<f32, String> {
  |                              ------------------- expected `Result<f32, String>` because of return type   
8 |     if b == 0.0 {
9 |       return Err("Division by zero".to_string());
  |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected enum `Result`, found enum `std::result::Result`
  |
  = note: expected enum `Result<f32, String>`
             found enum `std::result::Result<_, String>`

error[E0308]: mismatched types
  --> src\main.rs:16:9
   |
15 |     match result {
   |           ------ this expression has type `Result<f32, String>`
16 |         Ok(value) => {
   |         ^^^^^^^^^ expected enum `Result`, found enum `std::result::Result`
   |
   = note: expected enum `Result<f32, String>`
              found enum `std::result::Result<_, _>`

error[E0308]: mismatched types
  --> src\main.rs:19:9
   |
15 |     match result {
   |           ------ this expression has type `Result<f32, String>`
...
19 |         Err(message) => {
   |         ^^^^^^^^^^^^ expected enum `Result`, found enum `std::result::Result`
   |
   = note: expected enum `Result<f32, String>`
              found enum `std::result::Result<_, _>`

For more information about this error, try `rustc --explain E0308`.
error: could not compile `error-handling` due to 3 previous errors
1 Like

I’ve got it!
Remove the following from the code and it works correctly.

enum Result<ValueType, Error> {
    Ok(ValueType),
    Err(Error)
}
1 Like