函数中的泛型生命周期
fn longest(x: &str, y: &str) -> &str {
if x.len() > y.len() {
x
} else {
y
}
}
$ cargo run
Compiling chapter10 v0.1.0 (file:///projects/chapter10)
error[E0106]: missing lifetime specifier
--> src/main.rs:9:33
|
9 | fn longest(x: &str, y: &str) -> &str {
| ---- ---- ^ expected named lifetime parameter
|
= help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `x` or `y`
help: consider introducing a named lifetime parameter
|
9 | fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
| ++++ ++ ++ ++
For more information about this error, try `rustc --explain E0106`.
error: could not compile `chapter10` due to previous error
提示文本揭示了返回值需要一个泛型生命周期参数,因为 Rust 并不知道将要返回的引用是指向 x 或 y。事实上我们也不知道,因为函数体中 if 块返回一个 x 的引用而 else 块返回一个 y 的引用!
生命周期注解语法
生命周期注解并不改变任何引用的周期长短。当指定了泛型生命周期后函数也能接受任何生命周期的引用。生命周期注解描述了多个引用生命周期相互的关系,而不影响其生命周期。
生命周期注解有一个不太常见的语法:生命周期参数名称必须以撇号开头,其名称通常全是小写。'a
是大多数人默认使用的名称。生命周期参数注解位于引用的 **&**
之后,并有一个空格来将引用类型与生命周期注解分隔开。
&i32 // 引用
&'a i32 // 带有显式生命周期的引用
&'a mut i32 // 带有显式生命周期的可变引用
函数签名中的生命周期注解
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() {
x
} else {
y
}
}
现在函数签名表明对于某些生命周期 'a
,函数会获得两个参数,他们都是与生命周期 'a
存在的一样长的字符串 slice。它的实际含义是 **longest**
函数返回的引用生命周期与传入该函数的引用生命周期的较小者一致。
通过在函数签名中指定生命周期参数时,并没有改变任何传入值或返回值的生命周期,而是指出任何不满足这个约束条件的值都将被借用检查器拒绝。注意 longest
函数并不需要知道 x
和 y
具体会存在多久,而只需要知道有某个可以被 'a
替代的作用域将会满足这个签名。
当具体的引用被传递给 longest
时,被 'a
所替代的具体生命周期是 x 的作用域与 y 的作用域相重叠的那一部分。也就是说,是泛型生命周期 'a
的具体生命周期等同于 x
和 y
的生命周期中较小的那一个。
fn main() {
let string1 = String::from("long string is long");
{
let string2 = String::from("xyz");
let result = longest(string1.as_str(), string2.as_str());
println!("The longest string is {}", result);
}
}
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() {
x
} else {
y
}
}
string1 直到外部作用域结束都是有效的,string2 则在内部作用域中是有效的,而 result 则引用了一些直到内部作用域结束都是有效的值。借用检查器认可这些代码;它能够编译和运行,并打印出 The longest string is long string is long。
fn main() {
let string1 = String::from("long string is long");
let result;
{
let string2 = String::from("xyz");
result = longest(string1.as_str(), string2.as_str());
}
println!("The longest string is {}", result);
}
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() {
x
} else {
y
}
}
$ cargo run
Compiling chapter10 v0.1.0 (file:///projects/chapter10)
error[E0597]: `string2` does not live long enough
--> src/main.rs:6:44
|
6 | result = longest(string1.as_str(), string2.as_str());
| ^^^^^^^^^^^^^^^^ borrowed value does not live long enough
7 | }
| - `string2` dropped here while still borrowed
8 | println!("The longest string is {}", result);
| ------ borrow later used here
For more information about this error, try `rustc --explain E0597`.
error: could not compile `chapter10` due to previous error
错误表明为了保证 println!
中的 result 是有效的,string2 需要直到外部作用域结束都是有效的。Rust 知道这些是因为(longest)函数的参数和返回值都使用了相同的生命周期参数 'a
。
深入理解生命周期
fn longest<'a>(x: &'a str, y: &str) -> &'a str {
x
}
当从函数返回一个引用,返回值的生命周期参数需要与一个参数的生命周期参数相匹配。如果返回的引用 没有 指向任何一个参数,那么唯一的可能就是它指向一个函数内部创建的值,它将会是一个悬垂引用,因为它将会在函数结束时离开作用域。
fn main() {
let string1 = String::from("abcd");
let string2 = "xyz";
let result = longest(string1.as_str(), string2);
println!("The longest string is {}", result);
}
fn longest<'a>(x: &str, y: &str) -> &'a str {
let result = String::from("really long string");
result.as_str()
}
$ cargo run
Compiling chapter10 v0.1.0 (file:///projects/chapter10)
error[E0515]: cannot return reference to local variable `result`
--> src/main.rs:11:5
|
11 | result.as_str()
| ^^^^^^^^^^^^^^^ returns a reference to data owned by the current function
For more information about this error, try `rustc --explain E0515`.
error: could not compile `chapter10` due to previous error
出现的问题是 result 在 longest 函数的结尾将离开作用域并被清理,而我们尝试从函数返回一个 result 的引用。无法指定生命周期参数来改变悬垂引用,而且 Rust 也不允许我们创建一个悬垂引用。在这种情况,最好的解决方案是返回一个有所有权的数据类型而不是一个引用,这样函数调用者就需要负责清理这个值了。
结构体定义中的生命周期注解
struct ImportantExcerpt<'a> {
part: &'a str,
}
fn main() {
let novel = String::from("Call me Ishmael. Some years ago...");
let first_sentence = novel.split('.').next().expect("Could not find a '.'");
let i = ImportantExcerpt {
part: first_sentence,
};
}
这个结构体有一个字段,存放了一个字符串 slice,必须在结构体名称后面的尖括号中声明泛型生命周期参数,以便在结构体定义中使用生命周期参数。这个注解意味着 ImportantExcerpt 的实例不能比其 part 字段中的引用存在的更久。如果存在更久,就会出现垂直引用。
生命周期省略
每一个引用都有一个生命周期,而且需要为那些使用了引用的函数或结构体指定生命周期。
fn first_word(s: &str) -> &str {
let bytes = s.as_bytes();
for (i, &item) in bytes.iter().enumerate() {
if item == b' ' {
return &s[0..i];
}
}
&s[..]
}
fn main() {
let my_string = String::from("hello world");
// first_word works on slices of `String`s
let word = first_word(&my_string[..]);
let my_string_literal = "hello world";
// first_word works on slices of string literals
let word = first_word(&my_string_literal[..]);
// Because string literals *are* string slices already,
// this works too, without the slice syntax!
let word = first_word(my_string_literal);
}
这个函数没有生命周期注解却能编译是由于一些历史原因:在早期版本的 Rust 中,的确是不能编译的。每一个引用都必须有明确的生命周期。
fn first_word<'a>(s: &'a str) -> &'a str {
之后,Rust 团队发现需要加生命周期的场景是可预测的并且遵循几个明确的模式。接着 Rust 团队就把这些模式编码写进 Rust 编译器中。
被编码进 Rust 引用分析的模式被称为生命周期省略规则。如果 Rust 在明确遵守这些规则的前提下变量的生命周期仍然是模棱两可的话,它不会猜测剩余引用的生命周期应该是什么,会抛出一个错误,这可以通过增加对应引用之间相联系的生命周期注解来解决。
- 输入生命周期:函数或方法的参数的生命周期
- 输出生命周期:返回值的生命周期
编译器采用三条规则来判断引用何时不需要明确的注解:
每一个引用都有它自己的生命周期参数
有一个引用参数的函数有一个生命周期参数
fn foo<'a>(x: &'a i32)
,有两个引用参数的函数有两个不同的生命周期参数fn foo<'a, 'b>(x: &'a i32, y: &'b i32)
如果只有一个输入生命周期参数,那么它被赋予所有输出生命周期参数
fn foo<'a>(x: &'a i32) -> &'a i32
如果方法有多个输入生命周期参数并且其中一个参数是
**&self**
或**&mut self**
,说明是个对象的方法,那么所有输出生命周期参数被赋予**self**
的生命周期fn first_word(s: &str) -> &str {
根据第一条规则,签名变成这样:
fn first_word(s: &'a str) -> & str {
根据第二条规则,只有一个输入生命周期参数,所以其生命周期赋予所有的输出:
fn first_word(s: &'a str) -> &'a str {
再看一个例子:
fn longest(x: &str, y: &str) -> &str {
根据第一条规则:
fn longest(x: &'a str, y: &'b str) -> &str {
但它不适合第二条规则和第三条规则,所以就报错:编译器使用所有已知的生命周期省略规则,仍不能计算出签名中所有引用的生命周期。
方法定义中的生命周期注解
实现方法时,结构体字段的生命周期必须总是在
impl
关键字之后声明并在结构体名称之后被使用,因为这些生命周期是结构体类型的一部分呢。impl
块里的方法签名中,引用可能与结构体字段中的引用相关联,也可能是独立的。另外,生命周期省略规则也经常让我们无需在方法签名中使用生命周期注解。 ```rust struct ImportantExcerpt<’a> { part: &’a str, }
impl<’a> ImportantExcerpt<’a> { fn level(&self) -> i32 { 3 } }
// impl<’a> ImportantExcerpt<’a> { // fn announce_and_return_part(&self, announcement: &str) -> &str { // println!(“Attention please: {}”, announcement); // self.part // } // }
fn main() { let novel = String::from(“Call me Ishmael. Some years ago…”); let first_sentence = novel.split(‘.’).next().expect(“Could not find a ‘.’”); let i = ImportantExcerpt { part: first_sentence, }; }
`impl` 之后和类型名称之后的生命周期参数是必要的,不过因为第一条生命周期规则我们并不必须标注`self`引用的生命周期。
```rust
struct ImportantExcerpt<'a> {
part: &'a str,
}
impl<'a> ImportantExcerpt<'a> {
fn announce_and_return_part(&self, announcement: &str) -> &str {
println!("Attention please: {}", announcement);
self.part
}
}
fn main() {
let novel = String::from("Call me Ishmael. Some years ago...");
let first_sentence = novel.split('.').next().expect("Could not find a '.'");
let i = ImportantExcerpt {
part: first_sentence,
};
}
上述符合第三条生命周期省略规则:其中一个参数是&self
,返回值类型被赋予&self
的生命周期。
静态生命周期
'static
,其生命周期能够存活于整个程序期间。
let s: &'static str = "I have a static lifetime.";
这个字符串的文本被直接储存在程序的二进制文件中而这个文件总是可用的。因此所有的字符串字面值都是'static
的。
结合泛型类型参数、trait bounds 和生命周期
fn main() {
let string1 = String::from("abcd");
let string2 = "xyz";
let result = longest_with_an_announcement(
string1.as_str(),
string2,
"Today is someone's birthday!",
);
println!("The longest string is {}", result);
}
use std::fmt::Display;
fn longest_with_an_announcement<'a, T>(
x: &'a str,
y: &'a str,
ann: T,
) -> &'a str
where
T: Display,
{
println!("Announcement! {}", ann);
if x.len() > y.len() {
x
} else {
y
}
}
// Announcement! Today is someone's birthday!
// The longest string is abcd
ann
的类型是泛型 T
,可以被放入任何实现了 where
从句中指定的Display
trait 的类型。这个额外的参数会使用{}
打印,也就是为什么Display
trait bound 是必须的。因为生命周期也是泛型,所以生命周期参数'a
和泛型类型参数T
都位于函数名后的同一尖括号列表中。