-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
unexplained Rust gotcha in Guessing Game example #56681
Comments
Can you please re-file this against https://github.com/rust-lang/book? Thank you! |
Actually, I guess this really isn't about the contents of the book, it's more about the diagnostics. So let's leave it here. Sorry for the noise. |
good man. And I edited a little to pick up own mis-types... |
@steveklabnik I don't see how this is a diagnostics issue as it happens on runtime: someone writing this code from scratch would either have to deal with @narration-sd the book is using a blunt tool for error handling in order to keep the code simple. It is a very reasonable tradeoff. In real code you would deal with the result of
instead of failing outright when that
This is because it isn't a compile time error, and there are reasons for panics to not carry more information than they already do.
It is the name of the unwinding mechanism in rust, the result of calling
The error is pointing that the valid
Perfectly fair.
I believe you might be indeed misunderstanding where the problem is. It is not a type level problem, the string is still a valid string, the error is being caused when trying to call I believe there are two actionable points are:
|
hmm hmm. Nice analysis, @estebank; however, it doesn't seem in important regards to quite fit.
Here are a few things I thought of after the original issue:
Thanks, and Rust is interesting... |
It would be useful to have a copy of the code that was causing you trouble, that way we could devise further improvements to diagnostics that could help other newcomers. |
Sure -- as follows.
|
n.b. I've possibly made less clear by explaining too much, but the basis problem appears to be that once Then the parse fails, not because of an actual improper number, but because I did pick up your {:?} for better general error information...thanks. |
The problem you have here is not one of shadowing, but it is indeed a wart of the api:
You're appending to the buffer Filed #56734 to deal with the env var feedback. |
Fair enough -- though as you appear to recognize, the last things I'd expect from semantics of an api function named read_line are:
Then, github just updated on-the-page, so see you've picked up on first point, though 'identifying' seems it's short of the mark as far as how this should act, unless as ever I am missing something. Thanks, Esteban, and hope I'm contributing to development of clarity of Rust here. |
added to, if you are reading only email... |
ok, last on this I think. I had a look through old postings on Rust w/r/t read_line's behavior and related matters, not to say the doc. I guess all this I ran into is intentional, in stdio. And also non-intuitive to many of experience.
Ok, got that off my chest, and maybe I have to accept Rust as a specific-corners sort of language, useful for its uses however. Thanks for your patience to see this through, @estebank , and indeed shadowing itself seems to work as I'd thought it should in this case -- will have to read more about that in doc. Regards, |
p.s. for other unenlightened pre-rusters, this would have been helpful to go through. It does rather illustrate that I wasn't out on a limb to find things to be unusual...but also will help to get 'there'... http://dtrace.org/blogs/ahl/2015/06/22/first-rust-program-pain/ The various issues with read_line() are right up front, and he substitutes around them. As I don't think they're very comfortable to resolve. |
@estebank How is this code working after first iteration if we are shadowing the guess to u32? |
This is due to a facet you are promoting of Rust language, but causes consternation in what is supposed to be an easy-entry example.
The problem occurs when you are invited to 'just' add a loop to your code-so-far, to allow multiple guesses. What you are likely to get if you follow other language practices is a nasty error:
This occurs if you don't include the line
let mut guess = String::new();
within the loop, which is likely if you have a lot of experience, and are in a hurry, especially, though no doubt newer persons could also do it. If you go on to include the better error handling later in the lesson, you'll get an equally obscure error:invalid digit found in string
.In the morning, it seems this occurs because of the type-shadowing which has made the var
guess
no longer able to take a String. I would also say that the error messages, either of them, are very uninformative about the real problem.What can you do?
-- There's no line number for the code error -- this is basic, right?
-- 'panicked' ?? come on...
-- it's not an invalid digit, it's an invalid type String trying to be digits....you should say so
-- this whole thing gets awkward fast, so examining how you treat 'shadowed' variables would likely lead to much better thinking about how to act when it goes wrong.
-- So, this points to a fundamental Rust issue, I suspect.
-- the RUST_BACKTRACE is actually an env var, thus unclear -- and its presence in the message is by reading your prior issues a sort of bodge to respond to other obscurities...
-- the 'better' error message is just wrong. The input isn't bad because it's something wrong in the String; it's bad because the shadowed var can't take a String at all...
I was really surprised to see such a highly touted newer language being so raw. It makes me a bit wary about the intended reason to use it, Web Assembly. I hope you'll improve, thanks.
The text was updated successfully, but these errors were encountered: