In Rust, how do I use the implemented FromStr trait on BigInt?

I am trying to compile this program:

extern crate num;
use num::bigint::BigInt;
use std::from_str::FromStr;

fn main () {
    println!("{}", BigInt::from_str("1"));
}

But the conclusion from rustcis equal

testing.rs:6:20: 6:36 error: unresolved name `BigInt::from_str`.
testing.rs:6     println!("{}", BigInt::from_str("1"));
                                ^~~~~~~~~~~~~~~~
note: in expansion of format_args!
<std macros>:2:23: 2:77 note: expansion site
<std macros>:1:1: 3:2 note: in expansion of println!
testing.rs:6:5: 6:43 note: expansion site
error: aborting due to previous error

I suspect that I am doing something trivially wrong, but I tried to find examples and tried a bunch of different changes and tried nothing to work.

How do I change the source code so that it compiles?

+3
source share
3 answers

Answers that mention from_strwill not work with the latest versions of Rust, this feature has been removed.

A modern way of analyzing values ​​is a .parsemethod str:

extern crate num;
use num::bigint::BigInt;

fn main() {
    match "1".parse::<BigInt>() {
        Ok(n)  => println!("{}", n),
        Err(_) => println!("Error")
    }
}
+4
source
extern crate num;
use num::bigint::BigInt;

fn main () {
    println!("{}", from_str::<BigInt>("1"));
}

In function calls, you need to put ::in front of the angle brackets.

+4
source

, . .

extern crate num;
use num::bigint::BigInt;
use std::from_str::FromStr;

fn main () {
    let x : Result<BigInt,_> = FromStr::from_str("1");
    println!("{}", x);
}
0

Source: https://habr.com/ru/post/1624015/


All Articles