-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Type inference failure (partial hints?) #15960
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
Comments
I actually cover this in the guide: http://doc.rust-lang.org/guide.html Search for "It didn't work! Rust says "the type of this value must be known in this context." " |
You also go on to say:
Which doesn't work...
It also doesn't explain why It seems pretty arbitrary to me atm, sorry if this is simple misunderstanding |
No no, it's cool. If you don't understand, I haven't done my job. I just need to figure out how you don't understand, so I can fix the guide. When you say 'which doesn't work,' what do you mean, exactly? That last code snippet? Maybe something has changed in Rust in the last week or so, because when I wrote it, it did work. |
// Rust will fail to infer from the type of `f` that `random()` should be `random<int>();`
let f: int = std::rand::random() % 100i;
// But it will infer it when we do this: (although this is incorrect logically)
let f: int = 100i % std::rand::random(); Thus when you say we can use If there was something unclear about what I'm trying to demonstrate in my initial effort, I can try clear that up, but right now I feel like I'm repeating myself. If it did work as you say, then perhaps there is a bug? |
rust cannot infer the type of To prove this, let's make a hypothetical type
Now you're left with a conundrum, because
Or you can do:
In these cases you are explicitly saying what std::rand::random() should return. There is no ambiguity.
In this case, since there is a known type on the left side of the remainder operator, it becomes a lot easier for rust to infer the types of the rest of the line. |
Ah, thanks. This explanation makes sense. Closing as the issue now unrelated to Rust's inference |
Replace `option.map(cond) == Some(true)` with `option.is_some_and(cond)` Extracted from rust-lang#118253.
A small issue with hints and type inference, I don't fully grok what's going on so here is some code to better explain what I'm seeing:
However with:
rand::random<int>()
is successfully deduced.Why might this be the case?
The text was updated successfully, but these errors were encountered: