Announcing Rust 1.99.0 | Rust Blog

16 days ago by SorteKanin to c/rust

Empowering everyone to build reliable and efficient software.
ExperimentalGuy 6 points 16 days ago

Stable variadic functions is cool AF!

path: 0 26112349, hotness: undefined, score: 6, children: 0
arendjr 2 points 16 days ago

Vec::into_parts() returns a NonNull pointer, but isn’t the pointer allowed to be null if the vector is empty? I would expect that this method should be able to return a null pointer, but I’m probably missing something…

path: 0 26111286, hotness: undefined, score: 2, children: 5
SorteKanin 3 points 16 days ago

It's a good question. I believe Vec uses a near-zero pointer but not actually a null pointer for an empty Vec. I think part of the reason is that it makes for better optimisation of Option<Vec> which can then have the same size as Vec with the pointer being null signifying None.

Another big reason is that references are guaranteed not to be null and you can get a &[T] from an empty vec and that reference can't be null. Even in a non empty vec you need to consider this if T is zero sized.

path: 0 26111286 26111473, hotness: undefined, score: 3, children: 3
arendjr 2 points 16 days ago

Ah yes, that makes sense. Curious what it points to then when empty…

path: 0 26111286 26111473 26111649, hotness: undefined, score: 2, children: 2
SorteKanin 4 points 16 days ago

I looked it up, it's a pointer equal to the alignment of the type. So usually some small power of two.

path: 0 26111286 26111473 26111649 26111794, hotness: undefined, score: 4, children: 0
fil 2 points 16 days ago

Vec::<u8>::new().as_ptr() = 0x0000000000000001

playground

path: 0 26111286 26111473 26111649 26114052, hotness: undefined, score: 2, children: 0
BB_C 2 points 16 days ago

To add to the other answers. Usually, the easiest way to answer such questions is to click on source. Then in this case, you would immediately see this comment:

// SAFETY: A `Vec` always has a non-null pointer.
path: 0 26111286 26117865, hotness: undefined, score: 2, children: 0
rust
rust

@programming.dev

login for more options
8328
1423
339

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

!performance@programming.dev

Credits
  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

go to feed...