Why Rust refuses a loop C++ compiles | Borrowing and the borrow checker (Rust in the AI era, Part 4)
Step Into
0:00 / 0:00
Why Rust refuses a loop C++ compiles | Borrowing and the borrow checker (Rust in the AI era, Part 4)
189 просмотров · 3 дн. назад
Step Into
106 подписчиков
189 просмотров · 3 дн. назад
Our token counter has a loop that splits hyphenated words and adds the pieces to the list. C++ compiles it. Rust refuses. In this part we find out who is right: what a borrow is, the one rule behind the borrow checker (many shared references or one mutable reference, never both at once), the dangling pointer that rule prevents, the real compiler error and the fix, and lifetimes in plain words with one example.
Chapters
0:00 Rust refuses a loop C++ compiles
0:22 Ownership, and borrowing instead
0:52 Two kinds of reference
1:34 Inside the C++ loop
2:35 Through the borrow checker's eyes
3:06 The real error, and the fix
3:44 Lifetimes in plain words
4:37 One picture, and the next question
Next, Part 5: how does Rust handle things that may be missing, or may fail?
Sources
The Rust Programming Language, 4.2 References and Borrowing: https://doc.rust-lang.org/book/ch04-02-ref...
The Rust Programming Language, 10.3 Validating References with Lifetimes: https://doc.rust-lang.org/book/ch10-03-lif...
The Rustonomicon, References and Aliasing: https://doc.rust-lang.org/nomicon/referenc... and https://doc.rust-lang.org/nomicon/aliasing...
cppreference, std::vector::push_back (iterator invalidation): https://en.cppreference.com/w/cpp/containe...
The E0502 message is shown in the layout rustc prints for this kind of loop; the E0106 message is the Book's own. #rust #rustlang #programming