Rust finds wrong struct

I am trying to specify a Schema for polars::prelude::LazyCsvReader, as follows:

use polars::prelude::*;

let myschema = Schema::new(
        vec![
            Field::new("sepal_length", DataType::Float64),
            Field::new("sepal_width", DataType::Float64),
            Field::new("petal_length", DataType::Utf8),
            Field::new("petal_width", DataType::Float64),
            Field::new("species", DataType::Utf8),
        ]
    );

but I get the following error:

error[E0061]: this function takes 0 arguments but 1 argument was supplied
   --> src/main.rs:93:20
    |
93  |       let myschema = Schema::new(
    |                      ^^^^^^^^^^^
94  | /         vec![
95  | |             Field::new("sepal_length", DataType::Float64),
96  | |             Field::new("sepal_width", DataType::Float64),
97  | |             Field::new("petal_length", DataType::Utf8),
98  | |             Field::new("petal_width", DataType::Float64),
99  | |             Field::new("species", DataType::Utf8),
100 | |         ]
    | |_________- argument of type `Vec<polars::prelude::Field>` unexpected

I understand that this is due to the fact that my compiler knows about polars_core::Schema and not polars::prelude::Schema.

  • How can I ensure the correct struct method is used?
  • This isn't the first time happening to me with Rust: is there a general rule on which one of the many functions/structs with the same name is used?

Thanks

I'm not familiar with polars, but I just consulted the docs and those are the same type, whose new() takes zero parameters. Perhaps you meant Schema::from(vec![...])?

The general rule for use imports is that if you use a name imported by more than one * import, then you get a compilation error. If there is an explicit import and an * import, the explicit one wins.

Personally, I avoid * imports (except for re-exporting from modules that I defined) because I find it important for readability to be able to see where a name came from.

4 Likes

Thanks for the answer.

I mean that there are two different implementations of Schema::new() from here:

and one from here (which accepts an argument for new):

My compiler "finds" the polars_core::Schema::new() implementation although I have use polars::prelude::*;
What could be the reason?

The first link contains the docs for the latest version 0.26.1, in which new doesn't take arguments. The second link leads to the docs of version 0.10.1 which is over two years old (note the 0.10.1 in the URL as well as the "Go to latest version" link at the top). The version which you are using does not have a Schema::new that accepts arguments.

4 Likes

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.