Crate dyn_slice

source ·
Expand description

An implementation for a &dyn [Trait]-like type, inspired by a Reddit thread.

Indexing into a dyn slice yields a dyn object.


// Enable the required features (nightly must be used)

use dyn_slice::declare_new_fns;
use std::fmt::Display;

// Declare the `new` function
// (or you can use `dyn_slice::standard::display::new`
declare_new_fns!(display_slice Display);

fn main() {
    let array: [u8; 4] = [1, 2, 3, 4];

    // Create the first dyn slice
    let slice = display_slice::new(&array);

    let array2: [i16; 3] = [5, 6, 7];

    // Create the second dyn slice
    let slice2 = display_slice::new(&array2);

    // The iterators can be chained because they are iterators
    // over `&dyn Display` rather than over the underlying types
    let iter = slice.iter().chain(slice2.iter());
    for n in iter {

// Test the example (this can be ignored)
fn test() {

There are more examples in the examples directory.

Standard new dyn slice functions

There are some pre-made new functions for common traits in standard.


  • Dyn slice new and new_mut definitions for some common traits.