about summary refs log tree commit diff
path: root/src/doc
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2023-10-15 11:37:24 +0200
committerGitHub <noreply@github.com>2023-10-15 11:37:24 +0200
commitfb1f8dd95001c29cc4c9b757aa7976c26f252546 (patch)
tree5286e1c537a7e0b4a0819087ef4b69bd16f8a901 /src/doc
parente86e6b45e70cc30a9d1166ebc8ba7da05b9439d4 (diff)
parent318813def9fbc6ec75a4017d840e169a2bf5df0f (diff)
Rollup merge of #116741 - mejrs:string_pat, r=fee1-dead
Document `string_deref_patterns` feature

Rendered:
![image](https://github.com/rust-lang/rust/assets/59372212/aa3ef9e7-080d-4979-a363-3c24fe299c00)
Diffstat (limited to 'src/doc')
-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