1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-03-23 15:14:35 +02:00

Switch lifetime and variable names so they are different (#2586)

This helps clarify that the lifetime and variables names do not need to
match, but sticks to related themes (doc / document) for human clarity.

As suggested by @fw-immunant in
https://github.com/google/comprehensive-rust/pull/2585#pullrequestreview-2569184433,
which subsequently auto-merged.
This commit is contained in:
Dustin J. Mitchell 2025-01-23 17:55:59 -05:00 committed by GitHub
parent 9c03d51b74
commit a846003665
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -14,16 +14,16 @@ enum HighlightColor {
} }
#[derive(Debug)] #[derive(Debug)]
struct Highlight<'text> { struct Highlight<'document> {
slice: &'text str, slice: &'document str,
color: HighlightColor, color: HighlightColor,
} }
fn main() { fn main() {
let text = String::from("The quick brown fox jumps over the lazy dog."); let doc = String::from("The quick brown fox jumps over the lazy dog.");
let noun = Highlight { slice: &text[16..19], color: HighlightColor::Yellow }; let noun = Highlight { slice: &doc[16..19], color: HighlightColor::Yellow };
let verb = Highlight { slice: &text[20..25], color: HighlightColor::Pink }; let verb = Highlight { slice: &doc[20..25], color: HighlightColor::Pink };
// drop(text); // drop(doc);
println!("{noun:?}"); println!("{noun:?}");
println!("{verb:?}"); println!("{verb:?}");
} }
@ -35,7 +35,7 @@ fn main() {
underlying the contained `&str` lives at least as long as any instance of underlying the contained `&str` lives at least as long as any instance of
`Highlight` that uses that data. A struct cannot live longer than the data it `Highlight` that uses that data. A struct cannot live longer than the data it
references. references.
- If `text` is dropped before the end of the lifetime of `noun` or `verb`, the - If `doc` is dropped before the end of the lifetime of `noun` or `verb`, the
borrow checker throws an error. borrow checker throws an error.
- Types with borrowed data force users to hold on to the original data. This can - Types with borrowed data force users to hold on to the original data. This can
be useful for creating lightweight views, but it generally makes them somewhat be useful for creating lightweight views, but it generally makes them somewhat