hulud

4.5.2023

Implementing traits in a separate file

I had a problem implementing trait in a separate file. I had implemented the struct and the trait in my own modules, but when I tried to use the trait's function with the struct I got the following error:
error[E0599]: no method named `next_move` found for struct `Random` in the current scope
  -- src/main.rs:12:24
   |
12 |     game.make_move(eng.next_move(game));
   |                        ^^^^^^^^^ method not found in `Random`
   |
  ::: src/engine/mod.rs:5:8
   |
5  |     fn next_move(self, game: Game) - ChessMove;
   |        --------- the method is available for `Random` here
...
8  | pub struct Random { }
   | ----------------- method `next_move` not found for this struct
   |
   = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
   |
1  | use crate::engine::Engine;
   |
	

So the solution was to add use crate::engine::Engine; to main.rs, so that the scope where the function was called knew about the trait

Index

Archive

About Me