How do you actually use dynamically sized types in Rust?

前端 未结 3 1438
我寻月下人不归
我寻月下人不归 2020-11-30 10:13

In theory, Dynamically-Sized Types (DST) have landed and we should now be able to use dynamically sized type instances. Practically speaking, I can neither make it work, nor

3条回答
  •  甜味超标
    2020-11-30 10:28

    To amend the example that Paolo Falabella has given, here is a different way of looking at it with the use of a property.

    struct Foo<'a, T>
    where
        T: 'a + ?Sized,
    {
        printable_object: &'a T,
    }
    
    impl<'a, T> Print for Foo<'a, T>
    where
        T: 'a + ?Sized + fmt::Display,
    {
        fn print(&self) {
            println!("{}", self.printable_object);
        }
    }
    
    fn main() {
        let h = Foo {
            printable_object: "hello",
        };
        h.print();
    }
    

提交回复
热议问题