lotsoftools

Understanding Rust Error E0574

Error E0574 in Rust

Rust Error E0574 occurs when the code tries to use something other than a struct, variant, or union where one of these is expected. This error can be triggered in a variety of scenarios, such as when attempting to instantiate a module or bind a non-variant type in an enumeration.

Examples of Erroneous Code

Consider the following code block, which triggers Error E0574 because it attempts to instantiate the mordor module:

#![allow(unused)]
fn main() {
    mod mordor {}

    let sauron = mordor { x: () }; // error!

    enum Jak {
        Daxter { i: isize },
    }

    let eco = Jak::Daxter { i: 1 };
    match eco {
        Jak { i } => {} // error!
    }
}

Solutions for Error E0574

To resolve Rust Error E0574, ensure that a struct, variant, or union is used where expected. In the code example above, the first error can be fixed by modifying the instantiation of the mordor module to instantiate a type inside the module:

#![allow(unused)]
fn main() {
    mod mordor {
        pub struct TheRing {
            pub x: usize,
        }
    }

    let sauron = mordor::TheRing { x: 1 }; // ok!
}

In the second error of our initial erroneous code, the Jak enum is directly bound, which is not allowed. You can only bind one of its variants. Here's the corrected code to resolve the second error:

#![allow(unused)]
fn main() {
    enum Jak {
        Daxter { i: isize },
    }

    let eco = Jak::Daxter { i: 1 };
    match eco {
        Jak::Daxter { i } => {} // ok!
    }
}