about summary refs log tree commit diff
diff options
context:
space:
mode:
authorManish Goregaokar <manishsmail@gmail.com>2015-06-10 22:07:09 +0530
committerManish Goregaokar <manishsmail@gmail.com>2015-06-10 22:07:09 +0530
commitc84aff92b92b1d99af082c7dcb6f824c7a868ce1 (patch)
treeecc14302c0bbcdb5956fde6617de1d42af6e8de8
parent8025bc964c0f31726636811d97f9f46cd0d4c40f (diff)
parentb242a86864216e4d03bb1f5158afc2e9ffed9e21 (diff)
downloadrust-c84aff92b92b1d99af082c7dcb6f824c7a868ce1.tar.gz
rust-c84aff92b92b1d99af082c7dcb6f824c7a868ce1.zip
Rollup merge of #26142 - steveklabnik:gh25850, r=Gankro
Fixes #25850
-rw-r--r--src/doc/trpl/enums.md42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/doc/trpl/enums.md b/src/doc/trpl/enums.md
index 01905caf5ec..8ad4eeedd18 100644
--- a/src/doc/trpl/enums.md
+++ b/src/doc/trpl/enums.md
@@ -64,3 +64,45 @@ equality yet, but we’ll find out in the [`traits`][traits] section.
 [match]: match.html
 [if-let]: if-let.html
 [traits]: traits.html
+
+# Constructors as functions
+
+An enum’s 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], we’ll 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