rust-blog | Educational blog posts for Rust beginners | Blog library
kandi X-RAY | rust-blog Summary
kandi X-RAY | rust-blog Summary
I write educational content for Rust beginners and Rust advanced beginners. My posts are listed below in reverse chronological order. Note: Translations are community-maintained.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of rust-blog
rust-blog Key Features
rust-blog Examples and Code Snippets
Community Discussions
Trending Discussions on rust-blog
QUESTION
I'm reading Common Rust Lifetime Misconceptions to figure out how lifetime works, one of the example (posted below) really shocked me,
I can't convince myself why byte_1
and byte_2
still live well after the std::mem::drop(bytes);
is executed.
From my point of view, the references which byte_1
and byte_2
hold should be invalid after the iterator bytes
is dropped because the remainder
array is dropped as well, the compiler should treat the drop
action as an error, but in fact it pass the compiler and runs without problem...
Hope someone can give a reasonable explanation for a programmer coming from C++/C#, thanks!
...ANSWER
Answered 2021-Apr-08 at 05:58bytes
does not own b"1123"
. It only has a reference to it. Because there is no ownership, dropping bytes
doesn't drop b"1123"
. Dropping bytes
's reference doesn't affect other references with the same lifetime.
Now you could invalidate byte_1
and byte_2
if you were somehow able to drop the referent b"1123"
. But that's impossible. b"1123"
has a 'static
lifetime, and 'static
objects can't be dropped.
QUESTION
I am learning Rust and I am stuck with a toy example. I have already read documentation on lifetimes, this post and a bunch of questions on Stack Overflow. I've spent more than a week, but I'm still stuck, so I decided to ask for help from a community.
I have a generic trait BookSide
which returns BookIterator
(which extends usual Iterator
). And I have two implementations for both BookSide
and BookIterator
: ArrayBookSide
and CommissionBookSide
.
- First one is stateful. It has a
Vec
under the hood. - Second one is stateless: it wraps some other
BookSide
.
My goal is simply to compile the whole thing. I was solving problems and followed the suggestions of the compiler. This process resulted in the following code.
...ANSWER
Answered 2021-Jan-23 at 21:53Your issue essentially boils down to the following. What's the issue here?
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install rust-blog
Rust is installed and managed by the rustup tool. Rust has a 6-week rapid release process and supports a great number of platforms, so there are many builds of Rust available at any time. Please refer rust-lang.org for more information.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page