The following trait is implemented but not in scope issue

I'm new with rust

so I have my first crate https://crates.io/crates/swapi-client and found to use trait method need to import the trait first

on main.rs , it will work if I call it like this

// Trait
use swapi_client::RequestHandler;
// Structs
use swapi_client::{Film, People, Planet, Species, Starship, Vehicle};

fn main(){
  Planet::get(10)
}

in rust, is it possible to call the trait method without import the trait ? or above is normal ?

// Structs
use swapi_client::{Film, People, Planet, Species, Starship, Vehicle};

fn main(){
  Planet::get(10)
}

No. The above is normal. It takes a bit of getting used to, especially as the trait name you import may never actually be used.

1 Like

The reasoning for this is that multiple traits can define methods with the same name/signature. If this rule was not in place and there were multiple traits defining get for Planet, there would be no way for the compiler to know which one you wanted to use. Except for fully qualifying the method call, but that would also require you to import the trait!

2 Likes

Can't you use the fully-qualified name of the trait without importing it? (untested)

// Structs
use swapi_client::{Film, People, Planet, Species, Starship, Vehicle};

fn main(){
  <Planet as swapi_client::RequestHandler>::get(10)
}
1 Like

Ah, true. Still, it's a lot more verbose!

2 Likes

Thanks for the clarification.

since it's normal importing the trait then I'm fine with that :slightly_smiling_face:

<Planet as swapi_client::RequestHandler>::get(10)

above code is working fine, Glad to know this syntax.

Agree

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.