How to get the size of a user defined struct? (sizeof)

后端 未结 1 1085
一个人的身影
一个人的身影 2021-01-18 13:45

I\'ve got a structure with C representation:

struct Scard_IO_Request {
    proto: u32,
    pciLength: u32
}

when I want to ask the si

1条回答
  •  盖世英雄少女心
    2021-01-18 14:40

    For two reasons:

    1. There is no such function as "sizeof", so the compiler is going to have a rather difficult time calling it.

    2. That's not how you invoke generic functions.

    If you check the documentation for mem::size_of (which you can find even if you search for "sizeof"), you will see that it includes a runnable example which shows you how to call it. For posterity, the example in question is:

    fn main() {
        use std::mem;
        assert_eq!(4, mem::size_of::());
    }
    

    In your specific case, you'd get the size of that structure using

    mem::size_of::()
    

    0 讨论(0)
提交回复
热议问题