Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse BigInt from the num crate?

Tags:

rust

bigint

I am trying to use BigInt. My code is like this:

extern crate num;
use num::bigint::BigInt;
...
println!("{}", from_str::<BigInt>("1")); //this is line 91 in the code

In my Cargo.toml file I have the following:

[dependencies]
num = "0.1.30"

What I did seem to match what was said in this document, also this document and also an answer here on Stack Overflow.

However I got the following error:

Compiling example v0.1.0 (file:///C:/src/rust/example)
src\main.rs:91:20: 91:38 error: unresolved name `from_str` [E0425]
src\main.rs:91     println!("{}", from_str::<BigInt>("1"));
like image 534
Peter Pei Guo Avatar asked Jan 12 '16 22:01

Peter Pei Guo


People also ask

How to convert bigint to number in Java?

There are the following different ways we can convert BigInt to numeric values. Number constructor accepts object type and returns number data. the constructor takes bigint data and returns numbers. Following is an example for parse bigint to number. parseInt () method parses and object returns numeric value.

What is a bigint number?

A BigInt is a combination of BigUint and Sign. Common numerical operations are overloaded, so we can treat them the same way we treat other numbers. See the “Features” section for instructions for enabling random number generation.

How to convert bigint to number in typescript?

There are the following different ways we can convert BigInt to numeric values. Number constructor accepts object type and returns number data. the constructor takes bigint data and returns numbers. Following is an example for parse bigint to number. parseInt () method parses and object returns numeric value. The same above works in typescript.

What is a bigint in rust?

A BigInt is a combination of BigUint and Sign. Common numerical operations are overloaded, so we can treat them the same way we treat other numbers. See the “Features” section for instructions for enabling random number generation. The std crate feature is enabled by default, and is mandatory before Rust 1.36 and the stabilized alloc crate.


1 Answers

Figured out, seem like the current syntax is:

"8705702225074732811211966512111".parse::<BigInt>().unwrap();

Better yet, do the following:

match "8705702225074732811211966512111".parse::<BigInt>() {
    Ok(big) => {
        ...
like image 142
Peter Pei Guo Avatar answered Sep 28 '22 08:09

Peter Pei Guo