Would it be correct to say that Rust's ownership model and lifetimes are implementing RAII principle, but not for some external resource like a file or a database handler, but for all variables?
Ownership and destructors (Drop::drop()
) yes; lifetimes not so much.
I don't follow the distinction (if there is any). RAII can apply to all types and all kinds of resources, including dynamically allocated memory.
Right, thanks. Why not lifetimes though?
I'd say lifetimes are only indirectly related. They're necessary to make drop safe, but RAII only relies directly on drop.
Basically, if I understand correctly, according to RAII, it's still possible to drop an element of the struct, even before the rest of the struct is dropped.
The two things above aren't related, if I understand you. You can't only drop a field in a struct, unless you mean by taking out the contents of an Option, for example, and then dropping it.
When you say lifetime you may mean the duration/lifespan of an (owned) object, but Rust lifetime annotations are about the duration/lifespan of a reference to an object. I assumed (perhaps wrongly) you meant the latter.
The question is not "why not", it's "why would they"? Lifetimes don't influence the runtime behavior of code; they don't do anything.
Rust lifetimes (those '_
things) aren't the same as object lifetimes or the like. It's an unfortunate overlap of terminology. Think of them as the duration of borrows, a compile-time concept which is discarded after borrow check completes.
Ah, OK, thank you guys! Now it's clear!
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.