Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
dc7c8da7
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,发现更多精彩内容 >>
提交
dc7c8da7
编写于
10月 31, 2014
作者:
G
gamazeps
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Guide: explains the enum/match relationship
Closes #18169
上级
5e834243
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
50 addition
and
21 deletion
+50
-21
src/doc/guide.md
src/doc/guide.md
+50
-21
未找到文件。
src/doc/guide.md
浏览文件 @
dc7c8da7
...
...
@@ -1124,21 +1124,6 @@ enum OptionalInt {
Value(int),
Missing,
}
fn main() {
let x = Value(5);
let y = Missing;
match x {
Value(n) => println!("x is {}", n),
Missing => println!("x is missing!"),
}
match y {
Value(n) => println!("y is {}", n),
Missing => println!("y is missing!"),
}
}
```
This enum represents an
`int`
that we may or may not have. In the
`Missing`
...
...
@@ -1146,7 +1131,7 @@ case, we have no value, but in the `Value` case, we do. This enum is specific
to
`int`
s, though. We can make it usable by any type, but we haven't quite
gotten there yet!
You can have any number of values in an enum:
You can
also
have any number of values in an enum:
```
{rust}
enum OptionalColor {
...
...
@@ -1155,10 +1140,23 @@ enum OptionalColor {
}
```
Enums with values are quite useful, but as I mentioned, they're even more
useful when they're generic across types. But before we get to generics, let's
talk about how to fix these big
`if`
/
`else`
statements we've been writing. We'll
do that with
`match`
.
And you can also have something like this:
```
{rust}
enum StringResult {
StringOK(String),
ErrorReason(String),
}
```
Where a
`StringResult`
is either an
`StringOK`
, with the result of a computation, or an
`ErrorReason`
with a
`String`
explaining what caused the computation to fail. This kind of
`enum`
s are actually very useful and are even part of the standard library.
As you can see
`enum`
s with values are quite a powerful tool for data representation,
and can be even more useful when they're generic across types. But before we get to
generics, let's talk about how to use them with pattern matching, a tool that will
let us deconstruct this sum type (the type theory term for enums) in a very elegant
way and avoid all these messy
`if`
/
`else`
s.
# Match
...
...
@@ -1188,7 +1186,7 @@ expression will be evaluated. It's called `match` because of the term 'pattern
matching,' which
`match`
is an implementation of.
So what's the big advantage here? Well, there are a few. First of all,
`match`
do
es 'exhaustiveness checking.' Do you see that last arm, the one with the
enforc
es 'exhaustiveness checking.' Do you see that last arm, the one with the
underscore (
`_`
)? If we remove that arm, Rust will give us an error:
```
{ignore,notrust}
...
...
@@ -1255,6 +1253,37 @@ version, if we had forgotten the `Greater` case, for example, our program would
have happily compiled. If we forget in the
`match`
, it will not. Rust helps us
make sure to cover all of our bases.
`match`
expressions also allow us to get the values contained in an
`enum`
(also known as destructuring) as follows:
```
{rust}
enum OptionalInt {
Value(int),
Missing,
}
fn main() {
let x = Value(5);
let y = Missing;
match x {
Value(n) => println!("x is {}", n),
Missing => println!("x is missing!"),
}
match y {
Value(n) => println!("y is {}", n),
Missing => println!("y is missing!"),
}
}
```
That is how you can get and use the values contained in
`enum`
s.
It can also allow us to treat errors or unexpected computations, for example, a
function that is not guaranteed to be able to compute a result (an
`int`
here),
could return an
`OptionalInt`
, and we would handle that value with a
`match`
.
As you can see,
`enum`
and
`match`
used together are quite useful!
`match`
is also an expression, which means we can use it on the right
hand side of a
`let`
binding or directly where an expression is
used. We could also implement the previous line like this:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录