rust - Rust 的通用 FromStr 对象可以做什么?

标签 rust

Rust 的 str类有一个 parse返回 FromStr 的方法目的。 parse是模板化的,因此从 str 解析的类型可以手动指定,例如"3".parse::<i32>()评估为(一个 Result 对象包含)32 位 int 3 .

但是没有指定类型本身似乎并不是一个错误。相反,我在尝试打印结果(通用/未指定)时收到错误 FromStr对象:

let foo = "3".parse();
match foo
{
    Ok(m) => println!("foo: {}", m),
    Err(e) => println!("error! {}", e)
}

不会在第一行给出错误;相反,我收到以下错误:

<anon>:24:12: 24:13 error: unable to infer enough type information about `_`; type annotations or generic parameter binding required [E0282]
<anon>:24         Ok(m) => println!("foo: {}", m),

(这里,第 24 行是带有 Ok(m) 的行。)

那么 m 是什么?这里?或者是“无法推断出足够的类型信息”错误实际上是由于 parse事实上,不能在没有类型说明符的情况下被调用,并且编译器直到第一行才捕获到错误 Ok实际使用类型?

最佳答案

Rust's str class has a parse method that returns a FromStr object.

停在这里,这是你的错误。

parse 不返回一个 FromStr 对象; FromStr 是一个 trait,如果您来自 OO 背景,它可以被认为是一个抽象类,并且您不能返回一个对象抽象类型:它是抽象的!

因此,parse 返回的是某种类型 T 的实例,它必须实现 FromStr 接口(interface)。

But failing to specify the type does not seem to be an error in itself. Instead, I get an error when trying to print the resulting (generic/unspecified) FromStr object

因为不可能有这样的通用/非特定的 FromStr 对象。必须(从上下文中)推断或明确说明具体类型,并且该类型必须实现 FromStr

So what is m here?

只有你知道它应该是什么,编译器不知道,因此提示它不知道该做什么:)

Or is the "unable to infer enough type information" error actually due to the fact that parse in fact can't be called without a type specifier, and the compiler just doesn't catch the error until the first line where the resulting Ok type is actually used?

基本上。

除了编译器在使用结果 Ok 的第一行之前没有捕获到错误之外,更多的是编译器在推断类型时立即考虑完整的功能.从编译器的角度来看,推断类型的实际线索是立即出现还是在 50 行之后出现并不重要,它只需要出现在当前函数体中即可。

从开发人员的角度来看,这可能会导致提示缺少类型源于一个奇怪的地方;这是类型推断的缺点之一。另一方面,编译器无法知道您希望将注解放在哪里。毕竟有很多可能性:

// Example 1: immediately specifying the type
fn main() {
    let foo = "3".parse::<i32>();
    match foo
    {
        Ok(m) => println!("foo: {}", m),
        Err(e) => println!("error! {}", e)
    }
}

// Example 2: partially specifying the result type
// Note: the "_" is deduced to be std::num::ParseIntError because
//       this is how `FromStr::Err` is defined for `i32`.
fn main() {
    let foo: Result<i32, _> = "3".parse();
    match foo
    {
        Ok(m) => println!("foo: {}", m),
        Err(e) => println!("error! {}", e)
    }
}

// Example 3: specifying the result type of unwrapping
fn doit() -> Result<(), std::num::ParseIntError> {
    let foo: i32 = try!("3".parse());
    println!("foo: {}", foo);
    Ok(())
}

fn main() {
    match doit()
    {
        Ok(_) => (),
        Err(e) => println!("error! {}", e)
    }
}

// Example 4: letting the type be inferred from a function call
fn callit(f: i32) {
    println!("f: {}", f);
}

fn main() {
    let foo = "3".parse();
    match foo
    {
        Ok(m) => callit(m),
        Err(e) => println!("error! {}", e)
    }
}

关于rust - Rust 的通用 FromStr 对象可以做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32687388/

相关文章:

rust - 为什么这个特征/实现不兼容 - 绑定(bind)生命周期与具体生命周期

pointers - 如何递归传递可变引用?

documentation - 如何方便地托管 crate 的最新文档?

syntax - 为什么在模式匹配中使用 `ref` 而不是星号?

c - Read raw C string to Rust...在此上下文中将有符号转换为无符号的正确方法是什么?

rust - 是否可以通过特征将值存储在结构中?

rust - 如何从测试模块调用不在模块内的函数?

asynchronous - 如何在回调中使用 Rust future?

node.js - "Hello world"从 Chrome 而非 curl 测量时,Rust 网络服务器比 Node 慢

json - 根据用户角色返回 JSON