Allowing Access to Fields in Inner Struct with Different Names in Rust
03:39 29 Jan 2026

I am trying to allow for the interface in an outer struct to allow using fields that it does not hold representing information of an inner struct. I will explain with an example because reading it myself it seems confusing.

struct Inner {
    number: u32,
}

struct Outer {
    data: Inner,
}

//Some Arcane Wizardry...

pub fn main {
    let out = Outer {
        data: Inner {
            number: 9,
        },
    };

    println!("The index is {}", out.index);
}

In the given example, it is kind of stupid, but in the case I have it is kind of a linked list where the type keeps of the information can change. Therefore I can't use something like Index . After searching (inspired by Arc ) I found that it is possible to do something like so:

impl Deref for Outer {
    type Target = Inner;

    fn deref(&self) -> &Self::Target {
        &self.data
    }
}

This would allow one to use out.number , and through implementing Deref for every link in the chain one would be able to access almost all data. However this has three problems: first is that it isn't recommended (or so I've read), second is that there will be inevitable collisions in the names of fields, and third is that it doesn't allow me to change the name for the field. So I tried the following:

struct DataHolder {
    index: u32,
}
impl Deref for Outer {
    type Target = DataHolder;

    fn deref(&self) -> &Self::Target {
        &DataHolder {
            index: self.data.number
        }
    }
}

This, somewhat obviously, doesn't work. Because a move occurs, however if I try to set index as a reference I need to provide a lifetime which Target doesn't allow. I have seen that AsRef could also be used, however I'm unable to get it to work as I want. I also know that I should be able to do it with raw pointers, but I'd like not to do that.

Is my endeavor doomed to fail? Or is there some way I could do this? Because I'm tired of writing out.prev.prev.prev... . I appreciate any help you can give.

rust struct reference