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

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?



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.

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

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

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

Hi guys, one more Rust by Example fails to build with Rust 2018: https://doc.rust-lang.org/stable/rust-by-example/mod/visibility.html

The solution is written in the error:

error: relative paths are not supported in visibilities on 2018 edition
–> src/main.rs:33:16
33 | pub(in my_mod) fn public_function_in_my_mod() {
| ^^^^^^ help: try: crate::my_mod

I’m leaving this here so that it gets indexed for the next guy who doesn’t read the whole error message :slight_smile:

1 Like