Skip to content

Commit 6af60d3

Browse files
committed
Fix nits, rework the second example of select (the one deliberately failing to compile)
1 parent f8335fb commit 6af60d3

File tree

1 file changed

+15
-33
lines changed

1 file changed

+15
-33
lines changed

src/libstd/keyword_docs.rs

+15-33
Original file line numberDiff line numberDiff line change
@@ -1680,19 +1680,15 @@ mod use_keyword {}
16801680
///
16811681
/// `where` can also be used for lifetimes.
16821682
///
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:
16851685
///
16861686
/// ```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
16881688
/// where
1689-
/// 'b: 'a,
1689+
/// 'long: 'short,
16901690
/// {
1691-
/// if second {
1692-
/// s2
1693-
/// } else {
1694-
/// s1
1695-
/// }
1691+
/// if second { s2 } else { s1 }
16961692
/// }
16971693
///
16981694
/// let outer = String::from("Long living ref");
@@ -1701,35 +1697,20 @@ mod use_keyword {}
17011697
/// let inner = String::from("Short living ref");
17021698
/// let shorter = &inner;
17031699
///
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);
17061702
/// }
17071703
/// ```
17081704
///
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:
17111708
///
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
17261711
/// {
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 }
17311713
/// }
1732-
/// assert_eq!(res, &outer);
17331714
/// ```
17341715
///
17351716
/// `where` can also be used to express more complicated constraints that cannot
@@ -1749,7 +1730,8 @@ mod use_keyword {}
17491730
/// ```
17501731
///
17511732
/// `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:
17531735
///
17541736
/// ```rust
17551737
/// # #![allow(dead_code)]

0 commit comments

Comments
 (0)