Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
rust
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
1
Issues
1
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
redox-os
rust
Commits
6af60d3e
Commit
6af60d3e
authored
Jul 24, 2020
by
Alexis Bourget
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix nits, rework the second example of select (the one deliberately failing to compile)
parent
f8335fb1
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
15 additions
and
33 deletions
+15
-33
src/libstd/keyword_docs.rs
src/libstd/keyword_docs.rs
+15
-33
No files found.
src/libstd/keyword_docs.rs
View file @
6af60d3e
...
...
@@ -1680,19 +1680,15 @@ mod use_keyword {}
///
/// `where` can also be used for lifetimes.
///
/// This compiles because
the lifetime of `longer` is superior to the lifetime
///
of `shorter`, thus the constraint is
respected:
/// This compiles because
`longer` outlives `shorter`, thus the constraint is
/// respected:
///
/// ```rust
/// fn select
_where<'a, 'b>(s1: &'a str, s2: &'b str, second: bool) -> &'a
str
/// fn select
<'short, 'long>(s1: &'short str, s2: &'long str, second: bool) -> &'short
str
/// where
/// '
b: 'a
,
/// '
long: 'short
,
/// {
/// if second {
/// s2
/// } else {
/// s1
/// }
/// if second { s2 } else { s1 }
/// }
///
/// let outer = String::from("Long living ref");
...
...
@@ -1701,35 +1697,20 @@ mod use_keyword {}
/// let inner = String::from("Short living ref");
/// let shorter = &inner;
///
/// assert_eq!(select
_where
(shorter, longer, false), shorter);
/// assert_eq!(select
_where
(shorter, longer, true), longer);
/// assert_eq!(select(shorter, longer, false), shorter);
/// assert_eq!(select(shorter, longer, true), longer);
/// }
/// ```
///
/// On the other hand, this will not compile: `shorter` does not have a lifetime
/// that respects the constraint imposed by the `select_where` functions.
/// On the other hand, this will not compile because the `where 'b: 'a` clause
/// is missing: the `'b` lifetime is not known to live at least as long as `'a`
/// which means this function cannot ensure it always returns a valid reference:
///
/// ```rust,compile_fail,E0597
/// # fn select_where<'a, 'b>(s1: &'a str, s2: &'b str, second: bool) -> &'a str
/// # where
/// # 'b: 'a,
/// # {
/// # if second {
/// # s2
/// # } else {
/// # s1
/// # }
/// # }
/// let outer = String::from("Long living ref");
/// let longer = &outer;
/// let res;
/// ```rust,compile_fail,E0623
/// fn select<'a, 'b>(s1: &'a str, s2: &'b str, second: bool) -> &'a str
/// {
/// let inner = String::from("Short living ref");
/// let shorter = &inner;
///
/// res = select_where(longer, shorter, false);
/// if second { s2 } else { s1 }
/// }
/// assert_eq!(res, &outer);
/// ```
///
/// `where` can also be used to express more complicated constraints that cannot
...
...
@@ -1749,7 +1730,8 @@ mod use_keyword {}
/// ```
///
/// `where` is available anywhere generic and lifetime parameters are available,
/// as can be seen in the [`Cow`](crate::borrow::Cow) from the standard library:
/// as can be seen with the [`Cow`](crate::borrow::Cow) type from the standard
/// library:
///
/// ```rust
/// # #![allow(dead_code)]
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment