Skip to content

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

Merged
merged 2 commits into from
Aug 8, 2014
Merged

Guide: generics #16273

merged 2 commits into from
Aug 8, 2014

Conversation

steveklabnik
Copy link
Member

No description provided.

@steveklabnik
Copy link
Member Author

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/int/f64

Copy link
Member Author

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:

@steveklabnik
Copy link
Member Author

I'm having a small issue coming up with a function that we should use instead of square root. Any ideas?

@parir
Copy link
Contributor

parir commented Aug 7, 2014

How about the inverse 1/x?

@steveklabnik
Copy link
Member Author

I like it.

@steveklabnik
Copy link
Member Author

Fixed. WDYT?

}
```

We don't want to take the inverse of a negative number, so we check
Copy link
Contributor

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol, sigh, fixing

@steveklabnik
Copy link
Member Author

Just pushed a fix for the zero thing.

an error:

```{notrust,ignore}
error: binary operation `<` cannot be applied to type `T`
Copy link
Contributor

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.

Copy link
Member Author

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/weren't/were

@bors bors closed this Aug 8, 2014
@bors bors merged commit dac73ad into rust-lang:master Aug 8, 2014
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:
Copy link
Contributor

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

@steveklabnik steveklabnik deleted the guide_generics branch October 25, 2017 18:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants