Skip to content

Commit

Permalink
Unrolled build for rust-lang#116741
Browse files Browse the repository at this point in the history
Rollup merge of rust-lang#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)
  • Loading branch information
rust-timer authored Oct 15, 2023
2 parents ab73de7 + 318813d commit ae6367c
Showing 1 changed file with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ae6367c

Please sign in to comment.