Skip to content
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

inverse<T>() example in the Generics/Traits documentation does not work in beta 1.0.0 #24325

Closed
carribus opened this issue Apr 11, 2015 · 0 comments

Comments

@carribus
Copy link

The inverse() function introduced in the Generics chapter and 'solved' in the Traits chapter does not actually work. Even after adding the #![feature(std_misc)] attribute to the crate, it refuses to compile:

#![feature(std_misc)]

use std::num::Float;

fn inverse<T: Float>(x: T) -> Result<T, String> {
    if x == Float::zero() { return Err("x cannot be zero!".to_string()) }

    let one: T = Float::one();
    Ok(one / x)
}

fn main() {
    let x = inverse(0.0);

    match x {
        Ok(x) => println!("The inverse of 25 is {}", x),
        Err(msg) => println!("Error: {}", msg)
    }
}

yields:

src/main.rs:2:5: 2:20 warning: use of deprecated item: replaced by inherent methods; use rust-lang/num for generics, #[warn(deprecated)] on by default
src/main.rs:2 use std::num::Float;
                  ^~~~~~~~~~~~~~~
src/main.rs:4:15: 4:20 warning: use of deprecated item: replaced by inherent methods; use rust-lang/num for generics, #[warn(deprecated)] on by default
src/main.rs:4 fn inverse<T: Float>(x: T) -> Result<T, String> {
                            ^~~~~
src/main.rs:1:1: 1:22 error: unstable feature
src/main.rs:1 #![feature(std_misc)]
              ^~~~~~~~~~~~~~~~~~~~~
note: this feature may not be used in the beta release channel
error: aborting due to previous error
Could not compile `generics`.

The solution should look like this:

extern crate num;
use num::Float;

fn inverse<T>(x: T) -> Result<T, String> where T: Float {
    if x == T::zero()   { return Err("x cannot be zero!".to_string()) }

    let one: T = T::one();
    Ok(one / x)
}

fn main() {
    let x = inverse(25.0);

    match x {
        Ok(x) => println!("The inverse of 25 is {}", x),
        Err(msg) => println!("Error: {}", msg)
    }
}

/*
Cargo.toml file must include this:

[dependencies]
num = "0.1.22"
*/
steveklabnik added a commit to steveklabnik/rust that referenced this issue Apr 18, 2015
Let's talk about generics first, since we use traits to bound them
in funtions.

Partially addresses rust-lang#24325
steveklabnik added a commit to steveklabnik/rust that referenced this issue Apr 18, 2015
steveklabnik added a commit to steveklabnik/rust that referenced this issue Apr 20, 2015
Let's talk about generics first, since we use traits to bound them
in funtions.

Partially addresses rust-lang#24325
steveklabnik added a commit to steveklabnik/rust that referenced this issue Apr 20, 2015
steveklabnik added a commit to steveklabnik/rust that referenced this issue Apr 20, 2015
Let's talk about generics first, since we use traits to bound them
in funtions.

Partially addresses rust-lang#24325

Fixes rust-lang#24271
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

No branches or pull requests

2 participants