Rust approaches error handling with the aim of being explicit about possible error conditions. It separates the concerns of “normal” return values and “error” return values by using a specific set of types for each concern. Rust’s primary tools for expressing these concepts are the Result
and Option
types, along with the unwrap
method.
-
The
Result
type: This is an enum that represents either successful (Ok
) or unsuccessful (Err
) computation. Functions that can fail generally return aResult
variant.Here’s a simple example:
fn divide(numerator: f64, denominator: f64) -> Result<f64, &str> { if denominator == 0.0 { Err("Cannot divide by zero") } else { Ok(numerator / denominator) } }
In this case, if you try to divide by zero, the function will return an error wrapped in the
Err
variant. Otherwise, it will return the division result wrapped in theOk
variant. -
The
Option
type: This is another enum that represents a value that could be something (Some
) or nothing (None
). It’s often used when you have a situation where a value could be absent or present.For example:
fn find_word_starting_with<'a, 'b>(sentence: &'a str, initial: &'b str) -> Option<&'a str> { for word in sentence.split_whitespace() { if word.starts_with(initial) { return Some(word); } } None }
This function tries to find a word starting with a specific string in a sentence. If it finds such a word, it returns
Some(word)
, otherwise, it returnsNone
. -
The
unwrap
method: BothOption
andResult
types have anunwrap
method, which is used to get the value out. If the instance ofOption
is aSome
variant,unwrap
will return the value inside theSome
. If the instance is aNone
,unwrap
will panic.Similarly, for
Result
, if the instance is anOk
,unwrap
will return the value inside theOk
. If the instance is anErr
,unwrap
will panic.let maybe_number: Option<i32> = Some(42); println!("{}", maybe_number.unwrap()); // Prints 42 let definitely_not_a_number: Option<i32> = None; println!("{}", definitely_not_a_number.unwrap()); // This will panic
It’s generally advised to avoid using
unwrap
in most situations, as it can lead to your program crashing. Instead, you should handle errors appropriately using pattern matching or methods likeunwrap_or
,unwrap_or_else
,expect
, etc., which allow you to provide alternative values or actions in case of an error. -
The
?
operator: This is a convenient way to propagate errors upwards in the call stack. When you use?
on aResult
value, it does pretty much the same as a match expression would do: if the value isOk
, it unwraps it; if it’sErr
, it returns it from the current function. This allows for more concise error handling.
fn main() {
match foo() {
Ok(_) => println!("Success"),
Err(e) => println!("Error: {}", e),
}
}
fn foo() -> Result<(), &'static str> {
bar()?;
Ok(())
}
fn bar() -> Result<(), &'static str> {
Err("Some error")
}
In the foo
function, bar()?
will return the error from bar
, thus ending the foo
function early.文章來源:http://www.zghlxwxcb.cn/news/detail-622684.html
These are the basic building blocks of error handling in Rust. Rust also provides advanced features like creating your own error types, using the From
trait for flexible error conversions, etc., but those are more advanced topics.文章來源地址http://www.zghlxwxcb.cn/news/detail-622684.html
use std::fs::File;
fn main() {
panic!("出錯了");
println!("Hello Rust");
// panic 程序立即退出,退出的時候調(diào)用者拋出退出原因。
// 數(shù)組越界
let v = vec!["Rust", "Programming", "Language"];
println!("{}", v[5]); // thread 'main' panicked at 'index out of bounds: the len is 3 but the index is 5'
// 打開文件出錯
let f = File::open("abc.txt");
println!("{:?}", f); // Err(Os { code: 2, kind: NotFound, message: "系統(tǒng)找不到指定的文件。" })
// unwrap expect
let result = is_even(6).unwrap();
println!("結(jié)果{}", result);
let result = is_even(11).unwrap();
println!("結(jié)果{}", result);
// unwrap()是Result<T, E>的方法,實例上調(diào)用此方法時,如果是Ok,就返回Ok中的對象
// 如果是Err枚舉,在運行時會panic
// expect()接收msg::&str作為參數(shù),可以自定義報錯信息。
}
fn is_even(no: i32) -> Result<bool, String> {
return if no % 2 == 0 {
Ok(true)
} else {
Err("輸入的值不是偶數(shù)".to_string())
};
}
到了這里,關(guān)于Rust- 錯誤處理的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!