Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
We /lightweight dependent types/ now, again, maybe
#![feature(const_generics, const_evaluatable_checked)]
#![allow(incomplete_features)]
// tested with rustc 1.49.0-nightly (91a79fb29 2020-10-07)
fn append<T: Copy + Default, const N: usize, const M: usize>(
xs: &[T; N], ys: &[T; M]) -> [T; N + M] {
let mut result = [T::default(); N + M];
result[..N].copy_from_slice(&xs[..]);
result[N..].copy_from_slice(&ys[..]);
result
}
fn main() {
let arr = append(&[1i32; 3], &[2i32; 4]);
dbg!(arr);
}
#![feature(const_generics, const_evaluatable_checked)]
#![allow(incomplete_features)]
// tested with rustc 1.49.0-nightly (91a79fb29 2020-10-07)
#[inline]
const fn add_em(x: usize, y: usize) -> usize {
x + y
}
#[inline]
fn append<T: Copy + Default, const N: usize, const M: usize>(
xs: &[T; N], ys: &[T; M]) -> [T; add_em(N, M)] {
let mut result = [T::default(); add_em(N, M)];
result[..N].copy_from_slice(&xs[..]);
result[N..].copy_from_slice(&ys[..]);
result
}
fn main() {
let arr = append(&[1i32; 3], &[2i32; 4]);
dbg!(arr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.