Lost in "Specifying Placeholder Types in Trait Definitions with Associated Types"


I can’t find a way to solve the following problem:

I implement a structure that contains a vector vec The implementation has a function that returns T if T is empty then it must be filled taking into account the type of T.
I’m trying to limit the types to String and u128.

What is the best approach?
The following code is what I have at the moment, but I can’t find a way to call the fill_in function of the FillIn Trait.

struct MyStruct<T>
    T: FillIn,
    data: Vec<T>,

trait FillIn {
    type Output;

    fn fill_in(&mut self) -> Self::Output;

impl FillIn for String {
    type Output = Vec<String>;

    fn fill_in(&mut self) -> Vec<String> {
        println!("refill String");
        vec!["str1".to_string(), "str2".to_string(), "str3".to_string()]

impl FillIn for u128 {
    type Output = Vec<u128>;

    fn fill_in(&mut self) -> Vec<u128> {
        println!("refill u128");
        vec![1u128, 2u128, 3u128]

impl<T> MyStruct<T>
    T: FillIn,
    fn new() -> MyStruct<T> {
        MyStruct { data: Vec::new() }

    fn get_item(&mut self) -> T {
        let item = self.data.pop();
        match item {
            Some(i) => i,
            None => panic!("How I do fill in for types String u128 ?? ??"),

fn main() {
    use std::sync::Once;

    static START: Once = Once::new();

    START.call_once(|| {
        // run initialization here

    let mut x: MyStruct<String> = MyStruct::new();

    let mut y: MyStruct<u128> = MyStruct::new();

    /* Should not compile
    let mut z : MyStruct<i128> = MyStruct::new();



You can try an approach like this.

1 Like

Yes ! thanks for your solution