简体   繁体   English

在将字符串解析为数字时,为什么会出现“在此上下文中必须知道此值的类型”的错误?

[英]Why do I get the error “the type of this value must be known in this context” when parsing a string to a number?

I'm not trying to write sophisticated code, I just want to understand what is (or is not) going on here. 我不是想编写复杂的代码,我只是想了解这里发生了什么(或不是什么)。 I checked other questions but they all had complicated situations and I think this situation is the simplest so far. 我检查了其他问题,但他们都有复杂的情况,我认为这种情况到目前为止最简单。

I have the following code: 我有以下代码:

let one_step: f32 = "4.0".parse().unwrap();
let extra_step: u32 = one_step as u32;

println!("{:?}", extra_step);

The way I see it, we have a &str , we parse it to a f32 and unwrap it. 我看到它的方式,我们有一个&str ,我们将它解析为f32并解开它。 Then we convert the f32 to a u32 . 然后我们将f32转换为u32

Why can't I just do this? 为什么我不能这样做? Isn't this, practically, the same thing? 实际上,这不是一回事吗?

let single_step: u32 = "4.0".parse().unwrap() as u32;

println!("{:?}", single_step);

If I try to run this code, I get this error: 如果我尝试运行此代码,我会收到此错误:

error[E0619]: the type of this value must be known in this context
 --> src/main.rs:6:27
  |
6 |     let single_step: u32 = "4.0".parse().unwrap() as u32;
  |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

It looks like something requires us to break the operation into two chunks. 看起来有些东西要求我们将操作分成两个块。

The thing is that parse isn't just defined for f32 . 问题是parse不只是为f32定义的。 parse can define all kind of types (specifically any type that implements FromStr ). parse可以定义所有类型的类型(特别是任何实现FromStr类型)。 So how does Rust know that parse should return f32 and not for some other type? 那么Rust怎么知道parse应该返回f32而不是其他类型呢?

In your first example it knows this because oneStep is declared to have type f32 , so Rust can infer that it should call parse with f32 as its type argument. 在你的第一个例子中,它知道这一点,因为oneStep被声明为具有类型f32 ,因此Rust可以推断它应该使用f32作为其类型参数调用parse In the second example f32 isn't mentioned anywhere in the code, so Rust couldn't possibly figure it out. 在第二个例子中,代码中的任何地方都没有提到f32 ,所以Rust无法弄明白。

Instead of inferring the type argument from the type of a variable, you can also pass it directly. 您可以直接传递它,而不是从变量类型推断类型参数。 That way it will work in a single step: 这样它只需一步即可:

let singleStep: u32 = "4.0".parse::<f32>().unwrap() as u32;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Scala:当我使用“Option”时,为什么会出现类型不匹配错误? - Scala: Why do I get a type mismatch error when I use “Option”? 为什么在将大于i32的数字存储到变量中时,我没有得到文字超出范围错误? - Why do I not get a literal out of range error when storing a number larger than an i32 into a variable? 为什么会出现错误“方法...类型未定义...”? - Why do I get an error “The method … is undefined for the type…”? . 为什么会出现以下错误? 类型“照片”不可分配给类型“从不” - . WHY DO I GET THE FOLLOWING ERROR? Type 'Photo' is not assignable to type 'never' 为什么必须在printf中(与putStr一起使用)为脚本而不是ghci声明数字类型? - Why must the number type be declared in printf (when used with putStr) for a script but not for ghci? 如何在Scheme中获取值的类型? - How do I get the type of a value in Scheme? 为什么我会得到一个错误类型,其中的参数被用于同一目的两次 - Why do I get an error type with an argument that is used twice for the same purpose 必须知道java瞬态变量类型才能进行序列化? - java transient variable type must be known for serialization? 为什么我会因为缺少类型注释而收到错误“trait bound FromStr is not满足”? - Why do I get the error “trait bound FromStr is not satisfied” because of a missing type annotation? 为什么它会产生一个错误,说“无效的参数值”,当我将DATE类型的参数传递给水晶报告时? - Why does it generates an error saying “Invalid Parameter Value”, When I pass an argument of DATE type to crystal reports?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM