Enumeration output by Debug

The following code is compiled (in particular, MyError is recognized as having trait debugging):

use std::str;
use std::fmt;

#[derive(Debug)]
enum MyError<F> where F: str::FromStr {
    Parse(F::Err),
    Space,
}

fn my_parse<F>(s: String) -> Result<F,MyError<F>>
    where F: str::FromStr {
    match s.len() {
        0 => Err(MyError::Space),
        _ => s.parse::<F>().map_err(|err| MyError::Parse(err)),
    }
}

fn my_force_parse<F>(s: String) -> F
    where F: str::FromStr, MyError<F>: fmt::Debug {
    my_parse::<F>(s).unwrap()
}

fn main() {
    println!("hi");
    let s = "nope".to_string();
    println!("{}", my_force_parse::<i64>(s));
}

      

But if I replace the where clause for my_force_parse

with

where F: str::FromStr

      

then it is not. Has the program failed to collect that MyError implements Debug from the # [getive (Debug)] attribute?

+3


source to share


1 answer


MyError

does not implement Debug

... unconditionally. Rather, it implements it as long as all the required common parameters also implement it. Basically, the attribute #[derive(Debug)]

expands like this:

impl<F> MyError<F> where F: Debug {
    ...
}

      



After all, if it F

doesn't implement Debug

, MyError

it cannot provide the implementation.

Also, it looks like it's where F: str::FromStr + fmt::Debug

also not enough. Presumably Rust is either not smart enough to understand that F: Debug

MyError<F>: Debug

or there is a problem with the assumption.

+2


source







All Articles