I am trying to get this program to compile:
extern crate num;
use num::bigint::BigInt;
use std::from_str::FromStr;
fn main () {
println!(\"{}\", BigInt:
The plain function from_str
has been removed in recent versions of Rust. This function is now only available as a method of the FromStr
trait.
The modern way to parse values is the .parse method of str
:
extern crate num;
use num::bigint::BigInt;
fn main() {
match "1".parse::<BigInt>() {
Ok(n) => println!("{}", n),
Err(_) => println!("Error")
}
}
extern crate num;
use num::bigint::BigInt;
fn main () {
println!("{}", from_str::<BigInt>("1"));
}
In function calls, you need to put ::
before the angle brackets.
Your original code works almost as-is:
use num::bigint::BigInt; // 0.2.0
use std::str::FromStr;
fn main() {
println!("{:?}", BigInt::from_str("1"));
}
You need to switch to std::str::FromStr
and from_str
returns a Result
which requires the {:?}
(Debug
) formatter.
This works for calling the trait implementation directly instead of through the utility function. This is not idiomatic.
extern crate num;
use num::bigint::BigInt;
use std::from_str::FromStr;
fn main () {
let x : Result<BigInt,_> = FromStr::from_str("1");
println!("{}", x);
}