3 ms·
> how much you know about Rust Very little, so please bear with me ;) For example, from the linked page: > Most fundamentally, Vec is and always will be a (p
by saagarjha 9y ago
> how much you know about Rust
Very little, so please bear with me ;)
For example, from the linked page:
> Most fundamentally, Vec is and always will be a (pointer, capacity, length) triplet. No more, no less.
Does C++ also provide this guarantee?
> Vec will never perform a "small optimization" where elements are actually stored on the stack for two reasons:
Does C++ do this?
and from your response:
> No iterator invalidation from iterators you get from it
How are you guaranteeing this? C++ doesn't, so what is going on behind the scenes to make this work in Rust?
As you can see, I don't know enough about C++ as I would like either…
- Manishearth 9y ago> Does C++ also provide this guarantee? I don't think it does. It might. > Does C++ do this? IIRC C++ is allowed to perform the optimization. I don't recall if any current implementations do. They might. Edit: I'm wrong, it's not, however std::string is allowed to. > How are you guaranteeing this? C++ doesn't, so what is going on behind the scenes to make this work in Rust? Because Rust has an entire system of ownership and borrowing which exists at compile time to ensure these things don't happen.
- pbsd 9y agoC++ is not allowed to do the small buffer optimization for std::vector<T>. You'll find a number of small_vector<T> classes from third parties, though. The reason is that std::swap(v1, v2) must never invalidate existing iterators and/or references to elements into either vector, which the optimization would clearly break. std::string is the type you may be thinking of, where the standard does make a special exception to enable the SSO.
- Manishearth 9y agoaha! good to know. Yeah, I knew string can do SSO, so I assumed std::vector was allowed to as well.
- saagarjha 9y agoWell, I must be misunderstanding how iterators work in Rust, then. AFAIK in C++ an iterator is a thin wrapper around a pointer in memory, and resizing the container can cause the underlying "base" array to move around to somewhere else if it's reallocated–thus, the invalidation of the old iterator. What is a Rust iterator doing in this case?
- Manishearth 9y agoThe same thing. At compile time rust won't allow you to push to a vector if you have outstanding iterators.
- Xorlev 9y agoTo expand (hopefully correctly, I'm still learning Rust): You can have a single "mutable borrow" in a single lexical scope where you can mutate the vector _and its contents_ to your liking, but you can't pass a vector mid-mutation to another scope to read the vector while that borrow is active. It is safe to mutate the vector and its contents, as the vector owns the items inside it and there is only one active user of the collection. If you don't have a mutable borrow active, you can immutably borrow it as many times as you like and iterate over it to your heart's content in as many scopes as you like, but you can't mutate it. These borrows are lexically scoped. This is a start to explaining ownership: https://doc.rust-lang.org/book/second-edition/ch04-01-what-is-ownership.html https://doc.rust-lang.org/book/second-edition/ch04-01-what-i...
- kretin45 9y agoSo, sorry for being not bright. But if I may ask, if I have a Vec<B> A, then, it has I have a mutable reference to it in the form of A. Now, I take a const iterator to an object in A, say it, then, though the iterator is const for object of type B, it is a mutable reference for the Vec<B> A, and hence, I now cannot reallocate A till all the iterators have died(most probably because of scope). Is that true?
- Manishearth 9y agoI don't really understand the scenario you're putting out. Why would the iterator be a mutable reference? (there are mutable iterators but you explicitly said const). Basically, in Rust, this happens: let x = vec![1,2,3,4]; let iterator = vec.iter(); vec.push(5); // will not compile, until iterator goes out of scope
- blt 9y agoI'm pretty sure some cpp vector implementations use three pointers instead of length and capacity.
- saagarjha 9y agoIn the end, it's the same thing, isn't it? The amount of overhead is the same.
- blt 9y agoIn the article, it sounded like there is actually a guarantee that the (pointer, integer, integer) representation is used in rust. They are close to being equivalent, but maybe with weird-sized objects, the (pointer, pointer, pointer) version requires some divisions, and the other one only requires multiplications?
- angelsl 9y ago>> No iterator invalidation from iterators you get from it > How are you guaranteeing this? C++ doesn't, so what is going on behind the scenes to make this work in Rust? Rust's lifetimes.
- timClicks 9y agoIf you have 10 minutes or so, this is an excellent talk that explains how Rust's Vec<T> works by comparing it to Java and C++: https://youtu.be/ksirw1v5gXk?t=748 https://youtu.be/ksirw1v5gXk?t=748 (The rest of the talk is great too, but it's 50mins in total!)
- saagarjha 9y agoI haven't been exposed to how ownership/borrowing worked, so this video was great! Thanks for sharing it, since it made many of the other comments in this thread more clear.