-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
4 changed files
with
53 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -445,6 +445,8 @@ Oliver Scherer <[email protected]> <[email protected] | |
Oliver Scherer <[email protected]> <[email protected]> | ||
Oliver Scherer <[email protected]> <[email protected]> | ||
Oliver Scherer <[email protected]> | ||
Onur Özkan <[email protected]> <[email protected]> | ||
Onur Özkan <[email protected]> | ||
Ömer Sinan Ağacan <[email protected]> | ||
Ophir LOJKINE <[email protected]> | ||
Ožbolt Menegatti <[email protected]> gareins <[email protected]> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/doc/unstable-book/src/language-features/string-deref-patterns.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |