4 ms·
Congratulations! The highlight for me is stable conversion from Vec to array: let my_arr: [u32; 3] = my_vec.try_into().expect("msg");
by jph 6y ago
Congratulations! The highlight for me is stable conversion from Vec to array:
let my_arr: [u32; 3] = my_vec.try_into().expect("msg");
- simias 6y agoIndeed, I can't wait for const generics to become available outside of std, I've been bumping into that limitation since pre-rust-1.0 days, it'll be amazing to finally be able to rewrite all that hacky code correctly.
- nicoburns 6y agomin_const_generics is targeted for Rust 1.50 :)
- matt_kantor 6y agoFor anyone who wants to learn more: https://github.com/rust-lang/rust/pull/79135 https://github.com/rust-lang/rust/pull/79135
- wyldfire 6y agoHmm. Could you do this kind of code for a static allocation at compile-time too?
- efnx 6y agoI don’t think so - you can only use static functions to declare static variables. But you could do it with a macro. It’s debatable whether that would be readable and predictable, though.
- steveklabnik 6y agoI believe https://github.com/rust-lang/const-eval/issues/20 https://github.com/rust-lang/const-eval/issues/20 is the entry point into this kind of thing. As you can see, there's a lot of discussion. If it ever lands it'll be farther out.
- mark4 6y agoNewbie question. Why is this useful? Wouldn't slices be enough to fulfill the use-cases this would see?
- steveklabnik 6y agoSome things want an array of a specific size. If you have a function like https://doc.rust-lang.org/stable/std/primitive.u16.html#method.from_be_bytes https://doc.rust-lang.org/stable/std/primitive.u16.html#meth... pub const fn from_be_bytes(bytes: [u8; 2]) -> u16 you can't pass it a slice. This would let you pass a vector to this function. Now, because this wasn't possible previously, it means many things take a slice, and then check that the length is the length they expect, because that ends up being easier for folks. Stuff like this helps it be able to be done properly. (This specific function is one example that is done in this style, and it's a pain. I have real world code that looks like let foo = u16::from_be_bytes([some_slice[0], some_slice[1]]); This gets even worse with say, u128::from_be_bytes.)
- antonf 6y agoTryInto is already implemented for slices in 1.47, so `let foo = u16::from_be_bytes(some_slice.try_into().unwrap());` would work before 1.48. This trait wasn't implemented for vectors though, so you would have to write `let foo = u16::from_be_bytes(some_vec.to_slice().try_into().unwrap());`. With new release you can drop `to_slice()` part. Nothing earth-shattering, but makes sense.
- steveklabnik 6y agoAh fun! I missed that. Time to clean up some code...
- est31 6y ago> TryInto is already implemented for slices in 1.47, so `let foo = u16::from_be_bytes(some_slice.try_into().unwrap());` would work before 1.48. Rustdocs say it was 1.36 even? https://doc.rust-lang.org/nightly/std/primitive.array.html#impl-TryFrom%3C%26%27a%20%5BT%5D%3E https://doc.rust-lang.org/nightly/std/primitive.array.html#i...