[Rust | nvim] Rustlings 6.0.1, Part 10/16: Lifetimes and Tests

preview_player
Показать описание
[recorded on twitch, 11 July 2024]

This is my walkthrough of the Rustlings 6.0.1 release.

To install it yourself:
- then install rustlings by typing "cargo install rustlings"
- then run "rustlings init"
- then run "cd rustlings"
- then run "rustlings"

#rust #rustlang #rustlings #rustlings6.0.1
Рекомендации по теме
Комментарии
Автор

I believe that in the function longest(x: &str, y: &str) -> &str, the need for specifying lifetimes isn’t simply because there are two references. Instead, it arises because the compiler cannot infer which of the two references will be returned.

If longest returns the string with the greater length, the compiler needs to ensure that the returned reference is valid for the duration of its use. Since the compiler doesn’t know in advance whether x or y will be returned, it requires you to explicitly define lifetimes to indicate that the returned reference must live at least as long as the input references.

ralph_d_youtuber