发散函数
发散函数永远不会返回。它们使用 !
标记,这是一个空类型。
与其他所有类型相反,这种类型不能被实例化,因为此类型可能拥有的所有可能值的集合为空。请注意,它与 ()
类型不同,后者恰好有一个可能的值。
例如,此函数像往常一样返回,尽管返回值中没有信息。
fn some_fn() {
()
}
fn main() {
let _a: () = some_fn();
println!("This function returns and you can see this line.");
}
与此函数相反,此函数永远不会将控制权返回给调用者。
#![feature(never_type)]
fn main() {
let x: ! = panic!("This call never returns.");
println!("You will never see this line!");
}
虽然这可能看起来像一个抽象的概念,但它实际上非常有用且方便。这种类型的主要优点是它可以强制转换为任何其他类型,这使得它在需要精确类型的情况下(例如在 match 分支中)变得通用。这种灵活性允许我们编写这样的代码
fn main() {
fn sum_odd_numbers(up_to: u32) -> u32 {
let mut acc = 0;
for i in 0..up_to {
// Notice that the return type of this match expression must be u32
// because of the type of the "addition" variable.
let addition: u32 = match i%2 == 1 {
// The "i" variable is of type u32, which is perfectly fine.
true => i,
// On the other hand, the "continue" expression does not return
// u32, but it is still fine, because it never returns and therefore
// does not violate the type requirements of the match expression.
false => continue,
};
acc += addition;
}
acc
}
println!("Sum of odd numbers up to 9 (excluding): {}", sum_odd_numbers(9));
}
它也是永远循环的函数(例如 loop {}
,如网络服务器)或终止进程的函数(例如 exit()
)的返回类型。