# Why does Rust still consider the file to be of type \`Result\` even after I've handled the error in the main function?

**URL:** <https://users.rust-lang.org/t/why-does-rust-still-consider-the-file-to-be-of-type-result-even-after-ive-handled-the-error-in-the-main-function/113749>\
**Category:** help\
**Created:** [July 2, 2024, 12:06am UTC](https://users.rust-lang.org/t/why-does-rust-still-consider-the-file-to-be-of-type-result-even-after-ive-handled-the-error-in-the-main-function/113749 "2024-07-02T00:06:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ChoaibMouhrach](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/choaibmouhrach/32/41621_2.png) [@ChoaibMouhrach](https://users.rust-lang.org/u/ChoaibMouhrach)\
**Post date:** [July 2, 2024, 12:06am UTC](https://users.rust-lang.org/t/why-does-rust-still-consider-the-file-to-be-of-type-result-even-after-ive-handled-the-error-in-the-main-function/113749/1 "2024-07-02T00:06:21Z")

</div>

Why does Rust still consider the file to be of type `Result` even after I've handled the error in the main function?

```rs
use std::{fs::File, io};

fn get_file(file_name: &str) -> Result<File, io::Error> {
    let file = File::open(file_name);

    let file_err = match file {
        Ok(file) => return Ok(file),
        Err(err) => err,
    };

    match file_err.kind() {
        io::ErrorKind::NotFound => File::create(file_name),
        _ => Err(file_err),
    }
}

fn main() {
    let file = get_file("random.txt");

    if let Err(err) = file {
        println!("Error: {}", err);
        return;
    }

    // Result<File, Error>
    println!("file {:?}", file);
}

```

---

<div class="post-metadata">

**Author:** ![jumpnbrownweasel](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/jumpnbrownweasel/32/17403_2.png) [@jumpnbrownweasel](https://users.rust-lang.org/u/jumpnbrownweasel)\
**Post date:** [July 2, 2024, 12:13am UTC](https://users.rust-lang.org/t/why-does-rust-still-consider-the-file-to-be-of-type-result-even-after-ive-handled-the-error-in-the-main-function/113749/2 "2024-07-02T00:13:40Z")

</div>

> [@ChoaibMouhrach](#):
>
> ```rust
> if let Err(err) = file {
> println!("Error: {}", err);
> return;
> }
> 
> ```

You extracted the error from the Result and dealt with it, but you didn't extract the file from the Result. One way to do it is:

```rust
    let file = match file {
        Ok(f) = f,
        Err(err) => {
            println!("Error: {}", err);
            return;
        }
    };

    // now `file` is type File

```

Or perhaps this is better:

```rust
    let file = match get_file("random.txt") {
        Ok(f) = f,
        Err(err) => {
            println!("Error: {}", err);
            return;
        }
    };

```

---

<div class="post-metadata">

**Author:** ![paramagnetic](https://avatars.discourse-cdn.com/v4/letter/p/ad7895/32.png) [@paramagnetic](https://users.rust-lang.org/u/paramagnetic)\
**Post date:** [July 2, 2024, 12:00pm UTC](https://users.rust-lang.org/t/why-does-rust-still-consider-the-file-to-be-of-type-result-even-after-ive-handled-the-error-in-the-main-function/113749/3 "2024-07-02T12:00:20Z")

</div>

It looks like you expect Rust to have flow-sensitive typing. It doesn't (and enum variants are not types – enums are sum types, not unions of types).

---

<div class="post-metadata">

**Author:** ![kornel](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/kornel/32/4823_2.png) [@kornel](https://users.rust-lang.org/u/kornel)\
**Post date:** [July 2, 2024, 12:01pm UTC](https://users.rust-lang.org/t/why-does-rust-still-consider-the-file-to-be-of-type-result-even-after-ive-handled-the-error-in-the-main-function/113749/4 "2024-07-02T12:01:25Z")

</div>

Rust doesn't do type analysis like TypeScript or new instanceof in Java.

Type of a variable stays the same, even if you checked what is in it.

Result/Err is not special in Rust. It's just an enum, like `enum R { A, B, C }`, so if you checked that `variable == R::A`, it wouldn't change it to be `R::B`.

You need to pattern match on Result::Ok to get the non-error value, or use ? (Try) operator that does this match itself.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [September 30, 2024, 12:01pm UTC](https://users.rust-lang.org/t/why-does-rust-still-consider-the-file-to-be-of-type-result-even-after-ive-handled-the-error-in-the-main-function/113749/5 "2024-09-30T12:01:40Z")

</div>

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
