100-exercises-to-learn-rust icon indicating copy to clipboard operation
100-exercises-to-learn-rust copied to clipboard

add missing ; to return ()

Open kalmarek opened this issue 1 year ago • 2 comments

Note that all other remaining test functions do suppress return value with an explicit semicolon. However these two don't and the following (I guess still valid?) solution

    pub fn set_title(&mut self, title: String) -> &Self {
        if title.is_empty() {
            panic!("Title cannot be empty");
        } else if title.len() > 50 {
            panic!("Title cannot be longer than 50 bytes");
        }
        self.title = title;
        return self
    }

throws an error:

   Compiling setters v0.1.0 (/home/kalmar/local/rust/100-exercises-to-learn-rust/exercises/03_ticket_v1/07_setters)
error[E0308]: mismatched types
   --> exercises/03_ticket_v1/07_setters/src/lib.rs:110:9
    |
110 | /         Ticket::new(valid_title(), valid_description(), "To-Do".into())
111 | |             .set_title(overly_long_title())
    | |___________________________________________^ expected `()`, found `&Ticket`
    |
help: consider using a semicolon here
    |
111 |             .set_title(overly_long_title());
    |                                            +
help: try adding a return type
    |
109 |     fn title_cannot_be_longer_than_fifty_chars() -> &Ticket {
    |                                                  ++++++++++

For more information about this error, try `rustc --explain E0308`.
error: could not compile `setters` (lib test) due to 1 previous error

kalmarek avatar Sep 23 '24 13:09 kalmarek

Deploy Preview for taupe-lily-3539f6 ready!

Name Link
Latest commit 55896ab7144d1d3ce8c1857e0666203a46230061
Latest deploy log https://app.netlify.com/sites/taupe-lily-3539f6/deploys/66f170b59540640008be0e85
Deploy Preview https://deploy-preview-156--taupe-lily-3539f6.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

netlify[bot] avatar Sep 23 '24 13:09 netlify[bot]

The style recommended in the corresponding chapter returns (), so I don't think the test should allow for a different solution when going for the &mut self approach.

LukeMathWalker avatar Oct 22 '24 09:10 LukeMathWalker

All right, I missed the style recommendation @LukeMathWalker refers to, and so I agree that we should not allow returning anything other than () in the tests. Closing this PR

hdoordt avatar Oct 29 '24 13:10 hdoordt