Mention that enum constructors are functions

Fixes #25850
This commit is contained in:
Steve Klabnik 2015-06-09 16:12:43 -04:00
parent 7b0f2af27f
commit b242a86864

View File

@ -64,3 +64,45 @@ equality yet, but well find out in the [`traits`][traits] section.
[match]: match.html
[if-let]: if-let.html
[traits]: traits.html
# Constructors as functions
An enums constructors can also be used like functions. For example:
```rust
# enum Message {
# Write(String),
# }
let m = Message::Write("Hello, world".to_string());
```
Is the same as
```rust
# enum Message {
# Write(String),
# }
fn foo(x: String) -> Message {
Message::Write(x)
}
let x = foo("Hello, world".to_string());
```
This is not immediately useful to us, but when we get to
[`closures`][closures], well talk about passing functions as arguments to
other functions. For example, with [`iterators`][iterators], we can do this
to convert a vector of `String`s into a vector of `Message::Write`s:
```rust
# enum Message {
# Write(String),
# }
let v = vec!["Hello".to_string(), "World".to_string()];
let v1: Vec<Message> = v.into_iter().map(Message::Write).collect();
```
[closures]: closures.html
[iterators]: iterators.html