代码之家  ›  专栏  ›  技术社区  ›  Peter Pei Guo

如何从num板条箱解析BigInt?

  •  5
  • Peter Pei Guo  · 技术社区  · 9 年前

    我正在尝试使用 BigInt 。我的代码如下:

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

    在我的货物中。toml文件我有以下内容:

    [dependencies]
    num = "0.1.30"
    

    我的所作所为似乎与 this document , also this document 而且 an answer here on Stack Overflow .

    然而,我得到了以下错误:

    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"));
    
    1 回复  |  直到 7 年前
        1
  •  5
  •   Peter Pei Guo    9 年前

    经过计算,当前的语法似乎是:

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

    更好的做法是:

    match "8705702225074732811211966512111".parse::<BigInt>() {
        Ok(big) => {
            ...