about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-10-15 11:30:38 +0000
committerbors <bors@rust-lang.org>2023-10-15 11:30:38 +0000
commit4331c151b09a065f6b3bedf4158edc336317bc0d (patch)
tree6dbc1e845d180ebbc86e17d995a6c7f7fb01f75b /src
parentab73de7d7fddba62c4be542f29b0a8eed28a6bef (diff)
parent28739772b0775c82448e7422c5cee1127ca7f811 (diff)
Auto merge of #116757 - matthiaskrgr:rollup-3c25ogw, r=matthiaskrgr
Rollup of 4 pull requests

Successful merges:

 - #116594 (Fix `std::convert::TryFrom` doc)
 - #116741 (Document `string_deref_patterns` feature)
 - #116748 (Fix a spot I wrote the wrong word)
 - #116753 (add 'Onur Özkan' to .mailmap)

r? `@ghost`
`@rustbot` modify labels: rollup
Diffstat (limited to 'src')
-rw-r--r--src/doc/unstable-book/src/language-features/string-deref-patterns.md45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/doc/unstable-book/src/language-features/string-deref-patterns.md b/src/doc/unstable-book/src/language-features/string-deref-patterns.md
new file mode 100644
index 00000000000..3723830751e
--- /dev/null
+++ b/src/doc/unstable-book/src/language-features/string-deref-patterns.md
@@ -0,0 +1,45 @@
+# `string_deref_patterns`
+
+The tracking issue for this feature is: [#87121]
+
+[#87121]: https://github.com/rust-lang/rust/issues/87121
+
+------------------------
+
+This feature permits pattern matching `String` to `&str` through [its `Deref` implementation].
+
+```rust
+#![feature(string_deref_patterns)]
+
+pub enum Value {
+    String(String),
+    Number(u32),
+}
+
+pub fn is_it_the_answer(value: Value) -> bool {
+    match value {
+        Value::String("42") => true,
+        Value::Number(42) => true,
+        _ => false,
+    }
+}
+```
+
+Without this feature other constructs such as match guards have to be used.
+
+```rust
+# pub enum Value {
+#    String(String),
+#    Number(u32),
+# }
+#
+pub fn is_it_the_answer(value: Value) -> bool {
+    match value {
+        Value::String(s) if s == "42" => true,
+        Value::Number(42) => true,
+        _ => false,
+    }
+}
+```
+
+[its `Deref` implementation]: https://doc.rust-lang.org/std/string/struct.String.html#impl-Deref-for-String