[Solved] Example of Rust By Example can not be built with Rust 2018 Edition

#1

Hi.
The following two examples of Rust By Example succeeded in building on Rust 2015 Edition, but failed to build on 2018 Edition. And I did not understand how to solve it.
How do you get to succeed in building with 2018 Edition?

https://doc.rust-lang.org/rust-by-example/generics/assoc_items/the_problem.html

https://doc.rust-lang.org/rust-by-example/generics/assoc_items/types.html

error: expected one of `:` or `@`, found `,`
 --> src/main.rs:6:26
  |
6 |     fn contains(&self, &A, &B) -> bool; // Explicitly requires `A` and `B`.
  |                          ^ expected one of `:` or `@` here

error[E0407]: method `contains` is not a member of trait `Contains`
  --> src/main.rs:13:5
   |
13 | /     fn contains(&self, number_1: &i32, number_2: &i32) -> bool {
14 | |         (&self.0 == number_1) && (&self.1 == number_2)
15 | |     }
   | |_____^ not a member of trait `Contains`

error[E0599]: no method named `contains` found for type `Container` in the current scope
  --> src/main.rs:39:19
   |
1  | struct Container(i32, i32);
   | --------------------------- method `contains` not found for this
...
39 |         container.contains(&number_1, &number_2));
   |                   ^^^^^^^^
   |
   = help: items from traits can only be used if the trait is implemented and in scope
   = note: the following trait defines an item `contains`, perhaps you need to implement it:
           candidate #1: `std::ops::RangeBounds`

error: aborting due to 3 previous errors

Some errors occurred: E0407, E0599.
For more information about an error, try `rustc --explain E0407`.
error: Could not compile `rust_by_example`.

To learn more, run the command again with --verbose.
#2

You can put _: before &A and &B to get rid of the error.

#3

Anonymous parameters are no longer allowed in trait method declarations.

You can read about it here.

As @KrishnaSannasi says you can use an underscore as a replacement for variable names you don’t care about, eg.

fn contains(&self, _: &A, _: &B) -> bool;

1 Like
#4

@KrishnaSannasi, @harmic.
I could build it and I understood the reason why I had to change it.
Thanks for your advice!:smiley: