-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Guide: generics #16273
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
Guide: generics #16273
Conversation
Because the Traits section relies on the example from the generics section, I just pushed it up as a second commit here. |
let y: Result<f64, String> = Err("There was an error.".to_string()); | ||
``` | ||
|
||
This particular Result will return an `int` if there's a success, and a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/int/f64
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, i switched the example but not the text :embarassed:
I'm having a small issue coming up with a function that we should use instead of square root. Any ideas? |
How about the inverse 1/x? |
I like it. |
Fixed. WDYT? |
} | ||
``` | ||
|
||
We don't want to take the inverse of a negative number, so we check |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/a negative number/0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lol, sigh, fixing
Just pushed a fix for the zero thing. |
an error: | ||
|
||
```{notrust,ignore} | ||
error: binary operation `<` cannot be applied to type `T` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn't get this error anymore. Hmm.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same error just with ==. changing...
``` | ||
|
||
We don't want to take the inverse of zero, so we check to make sure that we | ||
weren't passed one. If we weren't, then we return an `Err`, with a message. If |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/weren't/were
This may seem like the Wild West, but there are two other restrictions around | ||
implementing traits that prevent this from getting out of hand. First, traits | ||
must be `use`d in any scope where you wish to use the trait's method. So for | ||
example, this does not work: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't find this paragraph or the following examples clear at all. I had to read it 20 times, compare both examples, try to decide what the problem is, decide why adding pub
fixes it.
- What about it doesn't work (apparently the lines where
pub
was added but you can't know that without comparing)? - The example is really big for you to not point out where the errors are
- The trait is in the module. How can it not be found? Why would the scope change?
- Comments detailing the issues would help
- Apparently needing
pub
should be obvious but I don't find it obvious
No description provided.