在 Rust 中,如何在 BigInt 上使用已实现的特征 FromStr? [英] In Rust, how do I use implemented trait FromStr on BigInt?

查看:63
本文介绍了在 Rust 中,如何在 BigInt 上使用已实现的特征 FromStr?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编译这个程序:

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::from_str("1"));
}

但是 rustc 的输出是

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 I am doing something trivially wrong, but I've tried searching for examples and tried a bunch of different changes and nothing I tried worked.

如何更改我的源代码以便编译?

How do I change my source code so this compiles?

推荐答案

普通函数 from_str 已在 Rust 的最新版本中删除.此函数现在仅可用作 FromStr trait 的方法.

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.

解析值的现代方法是 str的.parse方法:

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")
    }
}

这篇关于在 Rust 中,如何在 BigInt 上使用已实现的特征 FromStr?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆