函数调用中的生命周期

Lifetimes for function arguments and return values must be fully specified, but Rust allows lifetimes to be elided in most cases with a few simple rules. This is not inference — it is just a syntactic shorthand.

  • 每个没有生命周期注解的参数都会添加一个生命周期注解。
  • 如果只有一个参数生命周期,则将其赋予所有未加注解的返回值。
  • 如果有多个参数生命周期,但第一个是用于 self 的,则将该生命周期赋予所有未加注解的返回值。
  1. #[derive(Debug)]
  2. struct Point(i32, i32);
  3. fn cab_distance(p1: &Point, p2: &Point) -> i32 {
  4. (p1.0 - p2.0).abs() + (p1.1 - p2.1).abs()
  5. }
  6. fn nearest<'a>(points: &'a [Point], query: &Point) -> Option<&'a Point> {
  7. let mut nearest = None;
  8. for p in points {
  9. if let Some((_, nearest_dist)) = nearest {
  10. let dist = cab_distance(p, query);
  11. if dist < nearest_dist {
  12. nearest = Some((p, dist));
  13. }
  14. } else {
  15. nearest = Some((p, cab_distance(p, query)));
  16. };
  17. }
  18. nearest.map(|(p, _)| p)
  19. }
  20. fn main() {
  21. println!(
  22. "{:?}",
  23. nearest(
  24. &[Point(1, 0), Point(1, 0), Point(-1, 0), Point(0, -1),],
  25. &Point(0, 2)
  26. )
  27. );
  28. }

This slide should take about 5 minutes.

在此示例中,cab_distance 被轻易省略掉了。

nearest 函数提供了另一个函数示例,该函数的参数中包含多个引用,需要显式注解。

请尝试将签名调整为 “谎报”了返回的生命周期:

  1. fn nearest<'a, 'q>(points: &'a [Point], query: &'q Point) -> Option<&'q Point> {

This won’t compile, demonstrating that the annotations are checked for validity by the compiler. Note that this is not the case for raw pointers (unsafe), and this is a common source of errors with unsafe Rust.

Students may ask when to use lifetimes. Rust borrows always have lifetimes. Most of the time, elision and type inference mean these don’t need to be written out. In more complicated cases, lifetime annotations can help resolve ambiguity. Often, especially when prototyping, it’s easier to just work with owned data by cloning values where necessary.