How to initialize struct members in struct in constructor new
14:14 10 Aug 2026

I'm quite new with Rust (have experience with C/C++) and started to contribute to a bare-metal project for the purpose of education (to learn Rust) and also for fun. The project is targeting AArch64. Want to implement some basic cache and TLB maintenance functions.

The problem:

I want create a constructor for a crate which doesn't only set the private members (variables). They are unknown on compile time. First steps in the constructor is to retrieve information about the cache in inline assembly. The next step (what I want) is to initialize these private members with these information, for usage in the functions in this crate. I use the following structure for these information:

pub struct CacheInformation {
    cache_type: CacheType,
    cache_size: usize,
    line: u64,
    ways: u64,
    sets: u64
}

The type CacheType is a public enumeration:

pub enum CacheType {
    Instruction,
    DataLevel1,
    DataLevel2,
}

Private members for the implementation:

pub struct Cache {
    init_done: bool,
    i_cache: CacheInformation,
    d_l1_cache: CacheInformation,
    d_l2_cache: CacheInformation,
}

My current (not working - compiler error) from the new constructor:

impl Cache {
    /**
     * Cache Size Selection Register (CSSELR_EL1)
     */
    const CACHE_DATA: u64 = 0;
    const CACHE_INSTRUCTION: u64 = 1;
    const CACHE_LEVEL1: u64 = 0b000 << 1;
    const CACHE_LEVEL2: u64 = 0b001 << 1;       /* Only Data Cache has level 2 */

    /**
     * Cache Size ID Register (CCSIDR_EL1)
     */
    const CCSIDR_NUMSETS_MASK: u64 = 0x0FFF_E000;
    const CCSIDR_ASSOCIATIVITY_MASK: u64 = 0x0000_1FF8;
    const CCSIDR_LINESIZE_MASK: u64 = 0x0000_0003;


    pub fn new() -> Self {
        let mut csselr: u64;
        let mut ccsidr: u64;
        let mut i_cache_line: u64;
        let mut i_cache_sets: u64;
        let mut i_cache_ways: u64;
        let i_cache_size: usize;
        let mut d_l1_cache_line: u64;
        let mut d_l1_cache_sets: u64;
        let mut d_l1_cache_ways: u64;
        let mut d_l2_cache_line: u64;
        let mut d_l2_cache_sets: u64;
        let mut d_l2_cache_ways: u64;

        csselr = Self::CACHE_INSTRUCTION | Self::CACHE_LEVEL1;

        unsafe {
            core::arch::asm!(
                "msr CSSELR_EL1, {0}",
                "isb",
                "mrs {1}, CCSIDR_EL1",
                in(reg) csselr,
                out(reg) ccsidr);
        };

        i_cache_line = ccsidr & Self::CCSIDR_LINESIZE_MASK;
        i_cache_line *= 32;
        i_cache_sets = (ccsidr & Self::CCSIDR_NUMSETS_MASK) >> 13;
        i_cache_sets += 1;
        i_cache_ways = (ccsidr & Self::CCSIDR_ASSOCIATIVITY_MASK) >> 3;
        i_cache_ways += 1;
        i_cache_size = (i_cache_line * i_cache_sets * i_cache_ways) as usize;

        Cache {
            i_cache.line: i_cache_line,   // Compiler Error
        }
    }
}

Tried to initialize the implementation members (structures) with (but without success):

Cache {
            i_cache.line: i_cache_line,
        }

The compiler says I should try it this way, but also doesn't work:

error: expected one of `,`, `:`, or `}`, found `.`
   --> src/kernel/mmu.rs:114:20
    |
113 |         Cache {
    |         ----- while parsing this struct
114 |             i_cache.line: i_cache_line,
    |             -------^ expected one of `,`, `:`, or `}`
    |             |
    |             while parsing this struct field
    |
help: try naming a field
    |
114 |             i_cache: i_cache.line: i_cache_line,

What is the correct way to archive this, and is it even possible?

I think this question has already an answer, but currently I didn't find the correct one.

rust struct constructor