What is wrong with the code?

This is a playground: Rust Playground.

I know it gives many errors, but it prints out the things that are asked for. I will paste the error message from cloud evaluation below.

testErrors:    Compiling application v0.1.0 (/app)
error: no rules expected the token `:`
   --> tests/test.rs:103:22
    |
1   | macro_rules! impl_display {
    | ------------------------- when calling this macro
...
103 |     impl_display!(Dog: name);
    |                      ^ no rules expected this token in macro call

error: no rules expected the token `:`
   --> tests/test.rs:104:24
    |
1   | macro_rules! impl_display {
    | ------------------------- when calling this macro
...
104 |     impl_display!(Mouse: name, weight, age);
    |                        ^ no rules expected this token in macro call

error: no rules expected the token `:`
   --> tests/test.rs:105:27
    |
1   | macro_rules! impl_display {
    | ------------------------- when calling this macro
...
105 |     impl_display!(FullName: first last);
    |                           ^ no rules expected this token in macro call

warning: value assigned to `first` is never read
  --> src/main.rs:11:21
   |
11 |                     first = false;
   |                     ^^^^^
...
43 | impl_display!(Cat, name);
   | ------------------------ in this macro invocation
   |
   = help: maybe it is overwritten before being read?
   = note: `#[warn(unused_assignments)]` on by default
   = note: this warning originates in the macro `impl_display` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: value assigned to `first` is never read
  --> src/main.rs:11:21
   |
11 |                     first = false;
   |                     ^^^^^
...
44 | impl_display!(Dog, name);
   | ------------------------ in this macro invocation
   |
   = help: maybe it is overwritten before being read?
   = note: this warning originates in the macro `impl_display` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: value assigned to `first` is never read
  --> src/main.rs:11:21
   |
11 |                     first = false;
   |                     ^^^^^
...
45 | impl_display!(Mouse, name, age, weight);
   | --------------------------------------- in this macro invocation
   |
   = help: maybe it is overwritten before being read?
   = note: this warning originates in the macro `impl_display` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: value assigned to `first` is never read
  --> src/main.rs:11:21
   |
11 |                     first = false;
   |                     ^^^^^
...
46 | impl_display!(FullName, first, last);
   | ------------------------------------ in this macro invocation
   |
   = help: maybe it is overwritten before being read?
   = note: this warning originates in the macro `impl_display` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: fields `weight` and `age` are never read
  --> src/main.rs:22:5
   |
20 | struct Cat {
   |        --- fields in this struct
21 |     name: String,
22 |     weight: f64,
   |     ^^^^^^
23 |     age: u8,
   |     ^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: fields `weight` and `age` are never read
  --> src/main.rs:28:5
   |
26 | struct Dog {
   |        --- fields in this struct
27 |     name: String,
28 |     weight: f64,
   |     ^^^^^^
29 |     age: u8,
   |     ^^^

error[E0599]: `test_impl_display::Dog` doesn't implement `std::fmt::Display`
   --> tests/test.rs:129:20
    |
85  |     struct Dog {
    |     ----------
    |     |
    |     method `to_string` not found for this struct
    |     doesn't satisfy `test_impl_display::Dog: ToString`
    |     doesn't satisfy `test_impl_display::Dog: std::fmt::Display`
...
129 |     assert_eq!(dog.to_string(), "Innocentius".to_string());
    |                    ^^^^^^^^^ `test_impl_display::Dog` cannot be formatted with the default formatter
    |
    = note: the following trait bounds were not satisfied:
            `test_impl_display::Dog: std::fmt::Display`
            which is required by `test_impl_display::Dog: ToString`
note: the following trait must be implemented

error[E0599]: `test_impl_display::Mouse` doesn't implement `std::fmt::Display`
   --> tests/test.rs:130:22
    |
91  |     struct Mouse {
    |     ------------
    |     |
    |     method `to_string` not found for this struct
    |     doesn't satisfy `test_impl_display::Mouse: ToString`
    |     doesn't satisfy `test_impl_display::Mouse: std::fmt::Display`
...
130 |     assert_eq!(mouse.to_string(), "Hilarius, 0.03, 1".to_string());
    |                      ^^^^^^^^^ `test_impl_display::Mouse` cannot be formatted with the default formatter
    |
    = note: the following trait bounds were not satisfied:
            `test_impl_display::Mouse: std::fmt::Display`
            which is required by `test_impl_display::Mouse: ToString`
note: the following trait must be implemented

error[E0599]: `test_impl_display::FullName` doesn't implement `std::fmt::Display`
   --> tests/test.rs:131:21
    |
97  |     struct FullName {
    |     ---------------
    |     |
    |     method `to_string` not found for this struct
    |     doesn't satisfy `test_impl_display::FullName: ToString`
    |     doesn't satisfy `test_impl_display::FullName: std::fmt::Display`
...
131 |     assert_eq!(name.to_string(), "Maryna Vyazovska".to_string());
    |                     ^^^^^^^^^ `test_impl_display::FullName` cannot be formatted with the default formatter
    |
    = note: the following trait bounds were not satisfied:
            `test_impl_display::FullName: std::fmt::Display`
            which is required by `test_impl_display::FullName: ToString`
note: the following trait must be implemented

For more information about this error, try `rustc --explain E0599`.
error: could not compile `application` due to 6 previous errors
warning: build failed, waiting for other jobs to finish...
warning: `application` (bin "application" test) generated 6 warnings
warning: `application` (bin "application") generated 6 warnings (6 duplicates)

this is the excercise.

The starter code contains a macro from the previous exercise with two unfinished rules for implementing Display with more than one field to be displayed.

Complete the rules so that the macro works for any number of field names such that

  • impl_display!(Struct: field1 field2 field3); would produce implementation for printing the struct in the format "{field1} {field2} {field3}"
  • impl_display!(Struct: field1, field2, field3); would produce implementation for printing the struct in the format "{field1}, {field2}, {field3}"

There are no errors in your code, only warnings. Since it is only an exercise you can just ignore them by adding

#![allow(unused_assignments)]
#![allow(dead_code)]

at the top of your module. Example.

test still gives these 6 errors...

testErrors:    Compiling application v0.1.0 (/app)
error: no rules expected the token `:`
   --> tests/test.rs:106:22
    |
4   | macro_rules! impl_display {
    | ------------------------- when calling this macro
...
106 |     impl_display!(Dog: name);
    |                      ^ no rules expected this token in macro call

error: no rules expected the token `:`
   --> tests/test.rs:107:24
    |
4   | macro_rules! impl_display {
    | ------------------------- when calling this macro
...
107 |     impl_display!(Mouse: name, weight, age);
    |                        ^ no rules expected this token in macro call

error: no rules expected the token `:`
   --> tests/test.rs:108:27
    |
4   | macro_rules! impl_display {
    | ------------------------- when calling this macro
...
108 |     impl_display!(FullName: first last);
    |                           ^ no rules expected this token in macro call

error[E0599]: `test_impl_display::Dog` doesn't implement `std::fmt::Display`
   --> tests/test.rs:132:20
    |
88  |     struct Dog {
    |     ----------
    |     |
    |     method `to_string` not found for this struct
    |     doesn't satisfy `test_impl_display::Dog: ToString`
    |     doesn't satisfy `test_impl_display::Dog: std::fmt::Display`
...
132 |     assert_eq!(dog.to_string(), "Innocentius".to_string());
    |                    ^^^^^^^^^ `test_impl_display::Dog` cannot be formatted with the default formatter
    |
    = note: the following trait bounds were not satisfied:
            `test_impl_display::Dog: std::fmt::Display`
            which is required by `test_impl_display::Dog: ToString`
note: the following trait must be implemented

error[E0599]: `test_impl_display::Mouse` doesn't implement `std::fmt::Display`
   --> tests/test.rs:133:22
    |
94  |     struct Mouse {
    |     ------------
    |     |
    |     method `to_string` not found for this struct
    |     doesn't satisfy `test_impl_display::Mouse: ToString`
    |     doesn't satisfy `test_impl_display::Mouse: std::fmt::Display`
...
133 |     assert_eq!(mouse.to_string(), "Hilarius, 0.03, 1".to_string());
    |                      ^^^^^^^^^ `test_impl_display::Mouse` cannot be formatted with the default formatter
    |
    = note: the following trait bounds were not satisfied:
            `test_impl_display::Mouse: std::fmt::Display`
            which is required by `test_impl_display::Mouse: ToString`
note: the following trait must be implemented

error[E0599]: `test_impl_display::FullName` doesn't implement `std::fmt::Display`
   --> tests/test.rs:134:21
    |
100 |     struct FullName {
    |     ---------------
    |     |
    |     method `to_string` not found for this struct
    |     doesn't satisfy `test_impl_display::FullName: ToString`
    |     doesn't satisfy `test_impl_display::FullName: std::fmt::Display`
...
134 |     assert_eq!(name.to_string(), "Maryna Vyazovska".to_string());
    |                     ^^^^^^^^^ `test_impl_display::FullName` cannot be formatted with the default formatter
    |
    = note: the following trait bounds were not satisfied:
            `test_impl_display::FullName: std::fmt::Display`
            which is required by `test_impl_display::FullName: ToString`
note: the following trait must be implemented

For more information about this error, try `rustc --explain E0599`.
error: could not compile `application` due to 6 previous errors
warning: build failed, waiting for other jobs to finish...

You are making it very hard to help you if you don't share the code that is throwing the error you get. You just shared a playground that works.


impl_display!(Dog: name);

According to your macro definition, this should be

impl_display!(Dog, name);

instead.

i just copypasted the playground from my other code that i do the cloud test with :frowning:

I swear i have the commas etc in my code but the error still somehow says that i have ":" in the impl_display!(Dog, name); row for example. What could be a reason for that?

No idea. What is your development environment? Are you doing some online course where you write your code in some sort of editor in your browser? Could you try changing your macro to this and see if that helps?

Yes, the course is this one: Aalto FITech101 Courses. I dont know if you can access it. And your solution gives similar 6 errors :frowning:

And yes it is a web environment in browser. The excercise is in chapter 14 and it is the third one from the first set.

Ok so you can't just change the test suite to conform to your macro definition, but you have to change your macro definition to conform with the calling convention that is required of you by your assignment. Your macro must be callable like this:

impl_display!(Dog: name);

are you able to change your macro in a way that makes it work with a pattern type: fields,*, rather than type, fields,*? I.e. change the comma after the type to a colon.

well I don't really know how to do that :sweat_smile:

Okay, another hint. This line

($type: ty $(, $field: ident)*) => {

must be changed.

This page should contain all the information you need, I think.

this is now my code: Rust Playground

still gives this error in web.

testErrors:    Compiling application v0.1.0 (/app)
error: no rules expected the token `,`
   --> tests/test.rs:106:22
    |
4   | macro_rules! impl_display {
    | ------------------------- when calling this macro
...
106 |     impl_display!(Cat, name);
    |                      ^ no rules expected this token in macro call

error: no rules expected the token `,`
   --> tests/test.rs:108:30
    |
4   | macro_rules! impl_display {
    | ------------------------- when calling this macro
...
108 |     impl_display!(Mouse: name, weight, age);
    |                              ^ no rules expected this token in macro call

error[E0599]: `test_impl_display::Cat` doesn't implement `std::fmt::Display`
   --> tests/test.rs:132:20
    |
83  |     struct Cat {
    |     ----------
    |     |
    |     method `to_string` not found for this struct
    |     doesn't satisfy `test_impl_display::Cat: ToString`
    |     doesn't satisfy `test_impl_display::Cat: std::fmt::Display`
...
132 |     assert_eq!(cat.to_string(), "Felix".to_string());
    |                    ^^^^^^^^^ `test_impl_display::Cat` cannot be formatted with the default formatter
    |
    = note: the following trait bounds were not satisfied:
            `test_impl_display::Cat: std::fmt::Display`
            which is required by `test_impl_display::Cat: ToString`
note: the following trait must be implemented

error[E0599]: `test_impl_display::Mouse` doesn't implement `std::fmt::Display`
   --> tests/test.rs:134:22
    |
95  |     struct Mouse {
    |     ------------
    |     |
    |     method `to_string` not found for this struct
    |     doesn't satisfy `test_impl_display::Mouse: ToString`
    |     doesn't satisfy `test_impl_display::Mouse: std::fmt::Display`
...
134 |     assert_eq!(mouse.to_string(), "Hilarius, 0.03, 1".to_string());
    |                      ^^^^^^^^^ `test_impl_display::Mouse` cannot be formatted with the default formatter
    |
    = note: the following trait bounds were not satisfied:
            `test_impl_display::Mouse: std::fmt::Display`
            which is required by `test_impl_display::Mouse: ToString`
note: the following trait must be implemented

For more information about this error, try `rustc --explain E0599`.
error: could not compile `application` due to 4 previous errors
warning: build failed, waiting for other jobs to finish...

Those errors seem to be coming from some tests from the course you are following. Nothing is wrong with the code that you have shared in the playground.

1 Like

Yes, but now the error is a different one. Bear in mind your macro must match exactly the syntax that is required of you by your assignment. Now there is an unexpected , between your fields. Could you please re-read this section and see if you can figure out how to add a , between your fields as a separator?

Yes, the exercise is publicly visible. (Although it did force me to solve two other exercises first before I got to the relevant one :sweat_smile:.)

It seems you have somehow mixed up something between the second and third exercise, both featuring an impl_display macro, but with different syntax. For me, the website provides as a starting point this scaffolding for defining the macro, including test cases, and explicitly documenting comments of what’s expected for each example

macro_rules! impl_display {
    ($type: ty, $field: ident) => {
        impl ::std::fmt::Display for $type {
            fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
                write!(f, "{}", self.$field)
            }
        }
    };
    ($type: ty: $first: ident <rest of the fields>) => {
        impl ::std::fmt::Display for $type {
            fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
                write!(f, "{}", self.$first)?;
                // TODO: write the rest of the fields
                Ok(())
            }
        }
    };
    ($type: ty: $first: ident, <rest of the fields>) => {
        impl ::std::fmt::Display for $type {
            fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
                write!(f, "{}", self.$first)?;
                // TODO: write the rest of the fields
                Ok(())
            }
        }
    };
}

struct Cat {
    name: String,
    weight: f64,
    age: u8,
}

struct Dog {
    name: String,
    weight: f64,
    age: u8,
}

struct Mouse {
    name: String,
    weight: f64,
    age: u8,
}

struct FullName {
    first: String,
    last: String,
}

impl_display!(Cat, name);
impl_display!(Dog: name);
impl_display!(Mouse: name, age, weight);
impl_display!(FullName: first last);

fn main() {
    let cat = Cat {
        name: "Felix".to_string(),
        weight: 4.5,
        age: 5,
    };

    let dog = Dog {
        name: "Innocentius".to_string(),
        weight: 30.5,
        age: 3,
    };

    let mouse = Mouse {
        name: "Hilarius".to_string(),
        weight: 0.03,
        age: 1,
    };

    let name = FullName {
        first: "Pope".to_string(),
        last: "Linus".to_string(),
    };

    println!("{cat}"); // should print "Felix"
    println!("{dog}"); // should print "Innocentius"
    println!("{mouse}");  // should print "Hilarius, 1, 0.03"
    println!("{name}");  // should print "Pope Linus"
}

Maybe your mistake was that you tried copy-pasing the solution from the previous exercise, and expanding on it, instead of working with / starting with this provided code (e.g. by clicking the “Reset code” button on the website) and filling in the details from there.

2 Likes