@@ -1680,19 +1680,15 @@ mod use_keyword {}
1680
1680
///
1681
1681
/// `where` can also be used for lifetimes.
1682
1682
///
1683
- /// This compiles because the lifetime of `longer` is superior to the lifetime
1684
- /// of `shorter`, thus the constraint is respected:
1683
+ /// This compiles because `longer` outlives `shorter`, thus the constraint is
1684
+ /// respected:
1685
1685
///
1686
1686
/// ```rust
1687
- /// fn select_where<'a , 'b >(s1: &'a str, s2: &'b str, second: bool) -> &'a str
1687
+ /// fn select<'short , 'long >(s1: &'short str, s2: &'long str, second: bool) -> &'short str
1688
1688
/// where
1689
- /// 'b : 'a ,
1689
+ /// 'long : 'short ,
1690
1690
/// {
1691
- /// if second {
1692
- /// s2
1693
- /// } else {
1694
- /// s1
1695
- /// }
1691
+ /// if second { s2 } else { s1 }
1696
1692
/// }
1697
1693
///
1698
1694
/// let outer = String::from("Long living ref");
@@ -1701,35 +1697,20 @@ mod use_keyword {}
1701
1697
/// let inner = String::from("Short living ref");
1702
1698
/// let shorter = &inner;
1703
1699
///
1704
- /// assert_eq!(select_where (shorter, longer, false), shorter);
1705
- /// assert_eq!(select_where (shorter, longer, true), longer);
1700
+ /// assert_eq!(select (shorter, longer, false), shorter);
1701
+ /// assert_eq!(select (shorter, longer, true), longer);
1706
1702
/// }
1707
1703
/// ```
1708
1704
///
1709
- /// On the other hand, this will not compile: `shorter` does not have a lifetime
1710
- /// that respects the constraint imposed by the `select_where` functions.
1705
+ /// On the other hand, this will not compile because the `where 'b: 'a` clause
1706
+ /// is missing: the `'b` lifetime is not known to live at least as long as `'a`
1707
+ /// which means this function cannot ensure it always returns a valid reference:
1711
1708
///
1712
- /// ```rust,compile_fail,E0597
1713
- /// # fn select_where<'a, 'b>(s1: &'a str, s2: &'b str, second: bool) -> &'a str
1714
- /// # where
1715
- /// # 'b: 'a,
1716
- /// # {
1717
- /// # if second {
1718
- /// # s2
1719
- /// # } else {
1720
- /// # s1
1721
- /// # }
1722
- /// # }
1723
- /// let outer = String::from("Long living ref");
1724
- /// let longer = &outer;
1725
- /// let res;
1709
+ /// ```rust,compile_fail,E0623
1710
+ /// fn select<'a, 'b>(s1: &'a str, s2: &'b str, second: bool) -> &'a str
1726
1711
/// {
1727
- /// let inner = String::from("Short living ref");
1728
- /// let shorter = &inner;
1729
- ///
1730
- /// res = select_where(longer, shorter, false);
1712
+ /// if second { s2 } else { s1 }
1731
1713
/// }
1732
- /// assert_eq!(res, &outer);
1733
1714
/// ```
1734
1715
///
1735
1716
/// `where` can also be used to express more complicated constraints that cannot
@@ -1749,7 +1730,8 @@ mod use_keyword {}
1749
1730
/// ```
1750
1731
///
1751
1732
/// `where` is available anywhere generic and lifetime parameters are available,
1752
- /// as can be seen in the [`Cow`](crate::borrow::Cow) from the standard library:
1733
+ /// as can be seen with the [`Cow`](crate::borrow::Cow) type from the standard
1734
+ /// library:
1753
1735
///
1754
1736
/// ```rust
1755
1737
/// # #![allow(dead_code)]
0 commit comments