Compare commits

...

7 commits

Author SHA1 Message Date
sgasse 38c850ae74
Merge f3729f169e into 8d0aa11a35 2024-01-16 02:21:23 +09:00
liv 8d0aa11a35
Merge pull request #1826 from rust-lang/all-contributors/add-gerases
docs: add gerases as a contributor for content
2024-01-15 14:52:04 +00:00
allcontributors[bot] e2674498c6
docs: update .all-contributorsrc [skip ci] 2024-01-15 14:51:48 +00:00
allcontributors[bot] 3200581d4d
docs: update AUTHORS.md [skip ci] 2024-01-15 14:51:47 +00:00
liv 6afc4840b4
Merge pull request #1819 from gerases/grammar-fix
Correct for more standard English
2024-01-15 14:51:31 +00:00
Sergei Gerasenko 93aef73eb5 Correct for more standard English 2024-01-09 10:17:03 -06:00
Simon B. Gasse f3729f169e feat: add exercise involving lifetime elision 2023-10-16 20:31:24 +02:00
5 changed files with 86 additions and 1 deletions

View file

@ -2550,6 +2550,15 @@
"contributions": [ "contributions": [
"content" "content"
] ]
},
{
"login": "gerases",
"name": "gerases",
"avatar_url": "https://avatars.githubusercontent.com/u/8953623?v=4",
"profile": "https://github.com/gerases",
"contributions": [
"content"
]
} }
], ],
"contributorsPerLine": 8, "contributorsPerLine": 8,

View file

@ -360,6 +360,9 @@ authors.
<td align="center" valign="top" width="12.5%"><a href="https://github.com/neuschaefer"><img src="https://avatars.githubusercontent.com/u/1021512?v=4?s=100" width="100px;" alt="J. Neuschäfer"/><br /><sub><b>J. Neuschäfer</b></sub></a><br /><a href="https://github.com/rust-lang/rustlings/commits?author=neuschaefer" title="Code">💻</a></td> <td align="center" valign="top" width="12.5%"><a href="https://github.com/neuschaefer"><img src="https://avatars.githubusercontent.com/u/1021512?v=4?s=100" width="100px;" alt="J. Neuschäfer"/><br /><sub><b>J. Neuschäfer</b></sub></a><br /><a href="https://github.com/rust-lang/rustlings/commits?author=neuschaefer" title="Code">💻</a></td>
<td align="center" valign="top" width="12.5%"><a href="https://scooterhacking.org"><img src="https://avatars.githubusercontent.com/u/58905488?v=4?s=100" width="100px;" alt="Bastian Pedersen"/><br /><sub><b>Bastian Pedersen</b></sub></a><br /><a href="#content-bastianpedersen" title="Content">🖋</a></td> <td align="center" valign="top" width="12.5%"><a href="https://scooterhacking.org"><img src="https://avatars.githubusercontent.com/u/58905488?v=4?s=100" width="100px;" alt="Bastian Pedersen"/><br /><sub><b>Bastian Pedersen</b></sub></a><br /><a href="#content-bastianpedersen" title="Content">🖋</a></td>
</tr> </tr>
<tr>
<td align="center" valign="top" width="12.5%"><a href="https://github.com/gerases"><img src="https://avatars.githubusercontent.com/u/8953623?v=4?s=100" width="100px;" alt="gerases"/><br /><sub><b>gerases</b></sub></a><br /><a href="#content-gerases" title="Content">🖋</a></td>
</tr>
</tbody> </tbody>
</table> </table>

View file

@ -36,7 +36,7 @@ fn build_scores_table(results: String) -> HashMap<String, Team> {
let team_2_score: u8 = v[3].parse().unwrap(); let team_2_score: u8 = v[3].parse().unwrap();
// TODO: Populate the scores table with details extracted from the // TODO: Populate the scores table with details extracted from the
// current line. Keep in mind that goals scored by team_1 // current line. Keep in mind that goals scored by team_1
// will be the number of goals conceded from team_2, and similarly // will be the number of goals conceded by team_2, and similarly
// goals scored by team_2 will be the number of goals conceded by // goals scored by team_2 will be the number of goals conceded by
// team_1. // team_1.
} }

View file

@ -0,0 +1,54 @@
// lifetimes4.rs
//
// Sometimes, we have structs which hold on to data temporarily. A use-case of
// this could be a routing component which accepts data and returns it to
// another recipient. To avoid copying the data, we just accept a reference with
// lifetime and return this reference later.
//
// In the example below, we create a `Router` instance in a limited scope. It
// accepts a number reference created in the enclosing scope and returns it.
// In theory, this should be possible given that the number reference outlives
// the scope from which it is returned. However the borrow checker does not
// seem to understand it. What can we do about that?
//
// Execute `rustlings hint lifetimes4` or use the `hint` watch subcommand for a
// hint.
// I AM NOT DONE
struct Router<'a> {
number_ref: Option<&'a u64>,
}
impl<'a> Router<'a> {
fn new() -> Self {
Self { number_ref: None }
}
fn take_number_ref(&mut self, number_ref: &'a u64) {
self.number_ref = Some(number_ref);
}
fn return_number_ref(&mut self) -> Option<&u64> {
self.number_ref.take().take()
}
}
fn main() {
let number_ref = &123;
let returned_ref = {
// Create router within scope.
let mut router = Router::new();
// Accept number ref which lives longer than the router.
router.take_number_ref(number_ref);
// Return number ref which **should** live longer than the router.
router.return_number_ref()
};
if let Some(number) = returned_ref {
println!("The number is {number}");
}
}

View file

@ -879,6 +879,25 @@ hint = """
If you use a lifetime annotation in a struct's fields, where else does it need If you use a lifetime annotation in a struct's fields, where else does it need
to be added?""" to be added?"""
[[exercises]]
name = "lifetimes4"
path = "exercises/lifetimes/lifetimes4.rs"
mode = "compile"
hint = """
The compiler complains that the `Router` is dropped at the end of the smaller
scope while still being borrowed. However, we never intended to return a borrow
to the `Router`. What are we really returning from
`Router::return_number_ref`?
The method `Router::return_number_ref` only takes `&mut self`
and returns `Option<&u64>`. No explicit lifetimes are specified. What lifetime
will the borrow checker assume for the `&u64` in the `Option`? You may want to
re-read the chapter on lifetime elision:
https://doc.rust-lang.org/reference/lifetime-elision.html
What lifetime do we really want the `Option<&u64>` to have? Can we make that
explicit?"""
# TESTS # TESTS
[[exercises]] [[exercises]]