4 ms·
Nope, you're right, I need to apply the following diff (wrapped in a fn main() {}) to avoid rustc complaining: # diff -up test.rs test.fixed.rs --- te
by tlamponi 5y ago
Nope, you're right, I need to apply the following diff (wrapped in a fn main() {}) to avoid rustc complaining:
# diff -up test.rs test.fixed.rs
--- test.rs 2022-01-22 16:03:57.302742242 +0100
+++ test.fixed.rs 2022-01-22 16:03:27.766250377 +0100
@@ -2,13 +2,13 @@ fn main() {
// pretend that this was passed in on the command line
let my_number_string = String::from("42");
// If we just want to bubble up errors
- let my_number: u8 = &my_number_string.parse()?;
+ let my_number: u8 = my_number_string.parse()?;
assert_eq!(my_number, 42);
// If we might like to panic!
- let my_number: u8 = &my_number_string.parse().unwrap();
+ let my_number: u8 = my_number_string.parse().unwrap();
assert_eq!(my_number, 42);
// If we're a good Rustacean and check for errors before trying to use the data
- if let Ok(my_number: u8) = &my_number_string.parse() {
+ if let Ok(my_number) = my_number_string.parse::<u8>() {
assert_eq!(my_number, 42);
}
}
- tialaramex 5y agoOne of the best things to happen to C++ discussions was people starting to write godbolt links for their code. Immediately the code being discussed becomes code somebody actually compiled and maybe tried running and not just "oh, ignore the fact it's syntactically invalid - you know what I meant". No we don't. You can obviously write a godbolt link for Rust too, but Rust's playground is also a reasonable choice. I think that code maybe makes more sense with a turbofish for each parse call and type inference, but maybe that's just a matter of taste. If the author had used godbolt or playground or whatever they'd have written code that compiles and we'd not be guessing.