为何独立函数中的错误处理报错?Rust字符串转浮点数问题
字符串转浮点数函数的编译错误解决方法
问题代码
use std::error::Error; pub fn my_str2float(ss: &str) -> Result<f64, dyn Error> { let n = ss.parse::<f64>()?; Ok(n) } fn main (){ let numstr = "12.33"; match my_str2float(numstr) { Ok(num) => { println!("Number read: {}", num); } Err(e) => { println!("Error while reading number: {}", e.to_string()); } } }
编译错误信息
error[E0277]: the size for values of type `(dyn std::error::Error + 'static)` cannot be known at compilation time --> src/main.rs:4:40 | 4 | pub fn my_str2float(contents: &str) -> Result<f64, dyn Error> { | ^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | = help: the trait `Sized` is not implemented for `(dyn std::error::Error + 'static)` note: required by a bound in `Result` --> /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/core/src/result.rs:503:1 For more information about this error, try `rustc --explain E0277`. error: could not compile `testfns` due to previous error
问题分析
Rust 中Result<T, E>要求泛型参数E必须是Sized类型(编译期大小可知),而dyn Error是 trait 对象,属于非Sized类型——它只是对错误行为的抽象,实际对应的具体错误类型大小在编译期无法确定,因此触发编译错误。
解决方案
有两种常用修复方式:
方式1:用Box包裹trait对象
将返回类型改为Result<f64, Box<dyn Error>>,通过Box把 trait 对象装箱,变成编译期大小可知的指针类型:
use std::error::Error; pub fn my_str2float(ss: &str) -> Result<f64, Box<dyn Error>> { let n = ss.parse::<f64>()?; Ok(n) } fn main (){ let numstr = "12.33"; match my_str2float(numstr) { Ok(num) => { println!("Number read: {}", num); } Err(e) => { println!("Error while reading number: {}", e.to_string()); } } }
方式2:返回具体错误类型
str::parse::<f64>()返回的错误类型是ParseFloatError,直接指定这个具体类型作为错误返回值,完全规避类型擦除问题:
use std::num::ParseFloatError; pub fn my_str2float(ss: &str) -> Result<f64, ParseFloatError> { let n = ss.parse::<f64>()?; Ok(n) } fn main (){ let numstr = "12.33"; match my_str2float(numstr) { Ok(num) => { println!("Number read: {}", num); } Err(e) => { println!("Error while reading number: {}", e.to_string()); } } }
两种方式对比:
- 方式1灵活性高,适合后续函数可能返回多种不同错误类型的场景,能统一处理所有实现
Errortrait的错误。 - 方式2类型更明确,调用方可精准处理特定错误,避免类型擦除导致的错误信息丢失。
内容的提问来源于stack exchange,提问作者rnso
相关产品推荐
相关产品推荐

