100-exercises-to-learn-rust
100-exercises-to-learn-rust copied to clipboard
add missing ; to return ()
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
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...Use your smartphone camera to open QR code link. |
To edit notification comments on pull requests, go to your Netlify site configuration.
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.
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