Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR RUST

rust vec to array

///	It requires that the type `T` implement the `Copy``trait.
fn demo<T>(v: Vec<T>) -> [T; 32] where T: Copy {
    let slice = v.as_slice();
    let array: [T; 32] = match slice.try_into() {
        Ok(ba) => ba,
        Err(_) => panic!("Expected a Vec of length {} but it was {}", 32, v.len()),
    };
    array
}
Source by users.rust-lang.org #
 
PREVIOUS NEXT
Tagged: #rust #vec #array
ADD COMMENT
Topic
Name
4+6 =