Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document string_deref_patterns feature #116741

Merged
merged 1 commit into from
Oct 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading