Skip to content

Guide should have a simple example on how to do inverse #18182

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

Closed
JelteF opened this issue Oct 20, 2014 · 4 comments
Closed

Guide should have a simple example on how to do inverse #18182

JelteF opened this issue Oct 20, 2014 · 4 comments
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

Comments

@JelteF
Copy link
Contributor

JelteF commented Oct 20, 2014

At the end of the generics section it makes a jump to traits because the generic function for inverse doesn't work right away. I think it would be useful to add a working version of a generic inverse at the end of the traits section. This doesn't leave readers wondering what would have to be done in the other case.

@emberian emberian changed the title [Docs] Guide should have a simple example on how to do inverse Guide should have a simple example on how to do inverse Oct 20, 2014
@emberian emberian added A-docs E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Oct 20, 2014
@steveklabnik
Copy link
Member

I think this is a dup...

@elinorbgr
Copy link
Contributor

In fact, this might be a little more tricky than expected. A simple implementation would be something like:

use std::num::{Zero, One};
use std::ops::Div;

fn inverse<T: Eq + Div<T,T> + One + Zero>(x: T) -> Result<T, String> {
    if x == Zero::zero() { return Err("x cannot be zero!".to_string()); }

    Ok(One::one() / x)
}

But it yields the error:

<anon>:7:8: 7:18 error: the type of this value must be known in this context
<anon>:7     Ok(One::one() / x)
                ^~~~~~~~~~

I believe it is the same situation as here: http://stackoverflow.com/a/26305660/2536143

@mvdnes
Copy link
Contributor

mvdnes commented Oct 23, 2014

The example can be fixed as follows:

use std::num::{Zero, One};
use std::ops::Div;

fn inverse<T: Div<T,T> + One + Zero>(x: T) -> Result<T, String> {
    if x.is_zero() { return Err("x cannot be zero!".to_string()); }

    Ok(std::num::one::<T>() / x)
}

Using is_zero also eliminates the need for Eq.

@steveklabnik
Copy link
Member

Closing as a dup of #17224

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Projects
None yet
Development

No branches or pull requests

5 participants