Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
6f7faa0b
R
Rust
项目概览
int
/
Rust
大约 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
6f7faa0b
编写于
1月 05, 2015
作者:
A
Alex Crichton
浏览文件
操作
浏览文件
下载
差异文件
rollup merge of #19736: steveklabnik/gh19662
Fixes #19662.
上级
d46b8f1f
a56e7aee
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
77 addition
and
2 deletion
+77
-2
src/doc/guide-ownership.md
src/doc/guide-ownership.md
+77
-2
未找到文件。
src/doc/guide-ownership.md
浏览文件 @
6f7faa0b
...
...
@@ -230,8 +230,9 @@ fn add_one(num: &int) -> int {
```
Rust has a feature called 'lifetime elision,' which allows you to not write
lifetime annotations in certain circumstances. This is one of them. Without
eliding the lifetimes,
`add_one`
looks like this:
lifetime annotations in certain circumstances. This is one of them. We will
cover the others later. Without eliding the lifetimes,
`add_one`
looks like
this:
```
rust
fn
add_one
<
'a
>
(
num
:
&
'a
int
)
->
int
{
...
...
@@ -449,6 +450,80 @@ This is the simplest kind of multiple ownership possible. For example, there's
also
`Arc<T>`
, which uses more expensive atomic instructions to be the
thread-safe counterpart of
`Rc<T>`
.
## Lifetime Elision
Earlier, we mentioned 'lifetime elision,' a feature of Rust which allows you to
not write lifetime annotations in certain circumstances. All references have a
lifetime, and so if you elide a lifetime (like
`&T`
instead of
`&'a T`
), Rust
will do three things to determine what those lifetimes should be.
When talking about lifetime elision, we use the term 'input lifetime' and
'output lifetime'. An 'input liftime' is a lifetime associated with a parameter
of a function, and an 'output lifetime' is a lifetime associated with the return
value of a function. For example, this function has an input lifetime:
```
{rust,ignore}
fn foo<'a>(bar: &'a str)
```
This one has an output lifetime:
```
{rust,ignore}
fn foo<'a>() -> &'a str
```
This one has a lifetime in both positions:
```
{rust,ignore}
fn foo<'a>(bar: &'a str) -> &'a str
```
Here are the three rules:
*
Each elided lifetime in a function's arguments becomes a distinct lifetime
parameter.
*
If there is exactly one input lifetime, elided or not, that lifetime is
assigned to all elided lifetimes in the return values of that function..
*
If there are multiple input lifetimes, but one of them is
`&self`
or
`&mut
self`
, the lifetime of
`self`
is assigned to all elided output lifetimes.
Otherwise, it is an error to elide an output lifetime.
### Examples
Here are some examples of functions with elided lifetimes, and the version of
what the elided lifetimes are expand to:
```
{rust,ignore}
fn print(s: &str); // elided
fn print<'a>(s: &'a str); // expanded
fn debug(lvl: uint, s: &str); // elided
fn debug<'a>(lvl: uint, s: &'a str); // expanded
// In the preceeding example, `lvl` doesn't need a lifetime because it's not a
// reference (`&`). Only things relating to references (such as a `struct`
// which contains a reference) need lifetimes.
fn substr(s: &str, until: uint) -> &str; // elided
fn substr<'a>(s: &'a str, until: uint) -> &'a str; // expanded
fn get_str() -> &str; // ILLEGAL, no inputs
fn frob(s: &str, t: &str) -> &str; // ILLEGAL, two inputs
fn get_mut(&mut self) -> &mut T; // elided
fn get_mut<'a>(&'a mut self) -> &'a mut T; // expanded
fn args<T:ToCStr>(&mut self, args: &[T]) -> &mut Command // elided
fn args<'a, 'b, T:ToCStr>(&'a mut self, args: &'b [T]) -> &'a mut Command // expanded
fn new(buf: &mut [u8]) -> BufWriter; // elided
fn new<'a>(buf: &'a mut [u8]) -> BufWriter<'a> // expanded
```
# Related Resources
Coming Soon.
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录