Rustlings structs2.rs

Hello all, new in learning Rust and stuck at structs2.rs.

// structs2.rs
// Address all the TODOs to make the tests pass!

// I AM NOT DONE

#[derive(Debug)]
struct Order {
    name: String,
    year: u32,
    made_by_phone: bool,
    made_by_mobile: bool,
    made_by_email: bool,
    item_number: u32,
    count: u32,
}

fn create_order_template() -> Order {
    Order {
        name: String::from("Bob"),
        year: 2019,
        made_by_phone: false,
        made_by_mobile: false,
        made_by_email: true,
        item_number: 123,
        count: 0,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn your_order() {
        //let order_template = create_order_template();
        // TODO: Create your own order using the update syntax and template above!
        // let your_order =
        let your_order = create_order_template();
        assert_eq!(your_order.name, "Hacker in Rust");
        assert_eq!(your_order.year, create_order_template.year);
        assert_eq!(your_order.made_by_phone, create_order_template.made_by_phone);
        assert_eq!(your_order.made_by_mobile, create_order_template.made_by_mobile);
        assert_eq!(your_order.made_by_email, create_order_template.made_by_email);
        assert_eq!(your_order.item_number, create_order_template.item_number);
        assert_eq!(your_order.count, 1);
    }
}

this is the error i get:

error[E0609]: no field `year` on type `fn() -> Order {create_order_template}`
  --> exercises/structs/structs2.rs:40:59
   |
40 |         assert_eq!(your_order.year, create_order_template.year);
   |                                                           ^^^^

error[E0609]: no field `made_by_phone` on type `fn() -> Order {create_order_template}`
  --> exercises/structs/structs2.rs:41:68
   |
41 |         assert_eq!(your_order.made_by_phone, create_order_template.made_by_phone);
   |                                                                    ^^^^^^^^^^^^^

error[E0609]: no field `made_by_mobile` on type `fn() -> Order {create_order_template}`
  --> exercises/structs/structs2.rs:42:69
   |
42 |         assert_eq!(your_order.made_by_mobile, create_order_template.made_by_mobile);
   |                                                                     ^^^^^^^^^^^^^^

error[E0609]: no field `made_by_email` on type `fn() -> Order {create_order_template}`
  --> exercises/structs/structs2.rs:43:68
   |
43 |         assert_eq!(your_order.made_by_email, create_order_template.made_by_email);
   |                                                                    ^^^^^^^^^^^^^

error[E0609]: no field `item_number` on type `fn() -> Order {create_order_template}`
  --> exercises/structs/structs2.rs:44:66
   |
44 |         assert_eq!(your_order.item_number, create_order_template.item_number);
   |                                                                  ^^^^^^^^^^^

error: aborting due to 5 previous errors

For more information about this error, try `rustc --explain E0609`.

I dont understand why it can't see the fields of the fn create_order_template(). Rustc --explain 0609 says Attempted to access a non-existent field in a struct. Can someone guide me to my error?

The issue here is that create_order_template is a function, not a struct, so it doesn't have fields. You probably want to call the function to generate the struct by saying things like create_order_template().year (note the ()).

1 Like

That exercise is about learning the update syntax for initializing structs.

That means you have to instantiate a new struct from the one the exercise gave you.

Replace only that line with only one expression (the update syntax).

let your_order = /* you write what goes there */;
2 Likes

Hi there. I finally got the code compiling now thanks for the tips. I figured out the borrow/copy issue and changed some field data to match the test.

// structs2.rs
// Address all the TODOs to make the tests pass!

// Done May 30/21

#[derive(Debug)]
struct Order {
    name: String,
    year: u32,
    made_by_phone: bool,
    made_by_mobile: bool,
    made_by_email: bool,
    item_number: u32,
    count: u32,
}

fn create_order_template() -> Order {
    Order {
        name: String::from("Bob"),
        year: 2019,
        made_by_phone: false,
        made_by_mobile: false,
        made_by_email: true,
        item_number: 123,
        count: 0,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn your_order() {
        let order_template = create_order_template();
        // TODO: Create your own order using the update syntax and template above!
        // let your_order =
        //let your_order = Order;

		// added the ampersand after the variable let_order to solve the compiler error msg below
		// move occurs because `order_template` has type `Order`, which does not implement the `Copy` trait
        let your_order = &order_template;
        //assert_eq!(your_order.name, "Hacker in Rust");
        assert_eq!(your_order.name, "Bob");
        assert_eq!(your_order.year, order_template.year);
        assert_eq!(your_order.made_by_phone, order_template.made_by_phone);
        assert_eq!(your_order.made_by_mobile, order_template.made_by_mobile);
        assert_eq!(your_order.made_by_email, order_template.made_by_email);
        assert_eq!(your_order.item_number, order_template.item_number);
        //assert_eq!(your_order.count, 1);
        assert_eq!(your_order.count, 0);
    }
}

I'm sorry, you did not complete the exercise as it is intended. You changed the tests, you only have one thing to change :

let your_order = /*
   use the _update syntax_ here to create a new struct 
   from the one the test gives you with a new name a new count 
*/;

The update syntax looks like :

let your_order = Order { ..order_template };

See this paragraph here.

Geesh thats a fast check lol! Thanks i will redo my code :slight_smile:

Test failed after updating to new line code. I see the name strings are not matching thats why im tempted to change it in the testing code lol.

! Testing of exercises/structs/structs2.rs failed! Please try again. Here's the output:

running 1 test
test tests::your_order ... FAILED

successes:

successes:

failures:

---- tests::your_order stdout ----
thread 'tests::your_order' panicked at 'assertion failed: `(left == right)`
  left: `"Bob"`,
 right: `"Hacker in Rust"`', exercises/structs/structs2.rs:42:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    tests::your_order

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

The error is now just one after creating a new struct. Cant figure out the error message at line 29.

! Compiling of exercises/structs/structs2.rs failed! Please try again. Here's the output:
error: expected item, found keyword `let`
  --> exercises/structs/structs2.rs:29:1
   |
29 | let your_order = Order{ 
   | ^^^ expected item

error: aborting due to previous error

here's the full code file.

// structs2.rs
// Address all the TODOs to make the tests pass!

// Done May 30/21

#[derive(Debug)]
struct Order {
    name: String,
    year: u32,
    made_by_phone: bool,
    made_by_mobile: bool,
    made_by_email: bool,
    item_number: u32,
    count: u32,
}

fn create_order_template() -> Order {
    Order {
        name: String::from("Bob"),
        year: 2019,
        made_by_phone: false,
        made_by_mobile: false,
        made_by_email: true,
        item_number: 123,
        count: 0,
    }
}

let your_order = Order{ 
	name: String::from("Hacker in Rust"),
	count: 1,
	..order_template
};

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn your_order() {
        let order_template = create_order_template();
        // TODO: Create your own order using the update syntax and template above!

		// added the ampersand after the variable let_order to solve the compiler error msg below
		// move occurs because `order_template` has type `Order`, which does not implement the `Copy` trait
        //let your_order = &order_template;
        //let your_order = Order{ ..order_template };
        assert_eq!(your_order.name, "Hacker in Rust");
        assert_eq!(your_order.year, order_template.year);
        assert_eq!(your_order.made_by_phone, order_template.made_by_phone);
        assert_eq!(your_order.made_by_mobile, order_template.made_by_mobile);
        assert_eq!(your_order.made_by_email, order_template.made_by_email);
        assert_eq!(your_order.item_number, order_template.item_number);
        assert_eq!(your_order.count, 1);
        //assert_eq!(your_order.count, 0);
    }
}

That's the correct answer, move it to the correct scope now.

1 Like

Thank you. I moved the block inside the test function and it compiled. Great :slight_smile:

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.