Use of undeclared type that is defined in another file

914 views Asked by At

So, I have a hierarchy of files that have their own classes. Here is an example:

mod query;

struct Row<T>{
    data: Vec<Query<T>>,
}

impl<T> Row<T>{
    fn new(array: Vec<Query<T>>) -> Row<T>{
        Row{
          data: array,
        }
    }
}

Although it says the files are there, it says that "Query is an undeclared type," even when it exists in another file. The code works when everything is in the same file.

1

There are 1 answers

0
Shepmaster On BEST ANSWER

This is documented in the Rust book, specifically the section on modules. When you have different modules, you need to bring items from the other modules into scope using the use keyword.

mod query {
    pub struct Query;
}

// Bring Query into scope
use query::Query;

struct Row(Vec<Query>);

fn main() {}