Skip to content

Num::FromStrRadixErr should implement Debug #260

Closed
@ValouBambou

Description

@ValouBambou

I was writing a generic function for parsing one floating point number in each line of a string. And when I write the following function, I need to add this where clause for the code to compile.

use num_traits::{Float, Num};
use std::fmt::Debug;

fn parse_vec<F: Float>(content: &str) -> Vec<F>
where
    <F as Num>::FromStrRadixErr: Debug,
{
    content
        .lines()
        .map(|float| F::from_str_radix(float, 10).unwrap())
        .collect()
}

fn main() {
    let s = "1.0\n2.0\n3.0";
    let v: Vec<f32> = parse_vec(s);
    assert_eq!(v, vec![1.0, 2.0, 3.0]);
}

It's not really convenient because it propagates to all other generics I used. So, I think that enforcing FromStrRadixErr to implement debug by default is probably a good idea. But I may be wrong and there is maybe a good reason of not doing so.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions