-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(rust) fix regression with string matching
- Loading branch information
1 parent
40883e1
commit 93e6358
Showing
4 changed files
with
69 additions
and
4 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<span class="hljs-keyword">use</span> std::fmt::<span class="hljs-built_in">Debug</span>; <span class="hljs-comment">// Trait to bound with.</span> | ||
|
||
<span class="hljs-meta">#[derive(Debug)]</span> | ||
<span class="hljs-keyword">struct</span> <span class="hljs-title class_">Ref</span><<span class="hljs-symbol">'a</span>, T: <span class="hljs-symbol">'a</span>>(&<span class="hljs-symbol">'a</span> T); | ||
<span class="hljs-comment">// `Ref` contains a reference to a generic type `T` that has</span> | ||
<span class="hljs-comment">// some lifetime `'a` unknown by `Ref`. `T` is bounded such that any</span> | ||
<span class="hljs-comment">// *references* in `T` must outlive `'a`. Additionally, the lifetime</span> | ||
<span class="hljs-comment">// of `Ref` may not exceed `'a`.</span> | ||
|
||
<span class="hljs-comment">// A generic function which prints using the `Debug` trait.</span> | ||
<span class="hljs-keyword">fn</span> <span class="hljs-title function_">print</span><T>(t: T) <span class="hljs-keyword">where</span> | ||
T: <span class="hljs-built_in">Debug</span> { | ||
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"`print`: t is {:?}"</span>, t); | ||
} | ||
|
||
<span class="hljs-comment">// Here a reference to `T` is taken where `T` implements</span> | ||
<span class="hljs-comment">// `Debug` and all *references* in `T` outlive `'a`. In</span> | ||
<span class="hljs-comment">// addition, `'a` must outlive the function.</span> | ||
<span class="hljs-keyword">fn</span> <span class="hljs-title function_">print_ref</span><<span class="hljs-symbol">'a</span>, T>(t: &<span class="hljs-symbol">'a</span> T) <span class="hljs-keyword">where</span> | ||
T: <span class="hljs-built_in">Debug</span> + <span class="hljs-symbol">'a</span> { | ||
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"`print_ref`: t is {:?}"</span>, t); | ||
} | ||
|
||
<span class="hljs-keyword">fn</span> <span class="hljs-title function_">main</span>() { | ||
<span class="hljs-keyword">let</span> <span class="hljs-variable">x</span> = <span class="hljs-number">7</span>; | ||
<span class="hljs-keyword">let</span> <span class="hljs-variable">ref_x</span> = <span class="hljs-title function_ invoke__">Ref</span>(&x); | ||
|
||
<span class="hljs-title function_ invoke__">print_ref</span>(&ref_x); | ||
<span class="hljs-title function_ invoke__">print</span>(ref_x); | ||
} |
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,30 @@ | ||
use std::fmt::Debug; // Trait to bound with. | ||
|
||
#[derive(Debug)] | ||
struct Ref<'a, T: 'a>(&'a T); | ||
// `Ref` contains a reference to a generic type `T` that has | ||
// some lifetime `'a` unknown by `Ref`. `T` is bounded such that any | ||
// *references* in `T` must outlive `'a`. Additionally, the lifetime | ||
// of `Ref` may not exceed `'a`. | ||
|
||
// A generic function which prints using the `Debug` trait. | ||
fn print<T>(t: T) where | ||
T: Debug { | ||
println!("`print`: t is {:?}", t); | ||
} | ||
|
||
// Here a reference to `T` is taken where `T` implements | ||
// `Debug` and all *references* in `T` outlive `'a`. In | ||
// addition, `'a` must outlive the function. | ||
fn print_ref<'a, T>(t: &'a T) where | ||
T: Debug + 'a { | ||
println!("`print_ref`: t is {:?}", t); | ||
} | ||
|
||
fn main() { | ||
let x = 7; | ||
let ref_x = Ref(&x); | ||
|
||
print_ref(&ref_x); | ||
print(ref_x); | ||
} |