How to allocate structs on the heap without taking up space on the stack in stable Rust?

前端 未结 4 453
北荒
北荒 2020-12-19 07:45

In this code...

struct Test { a: i32, b: i64 }
    
fn foo() -> Box {              // Stack frame:
            


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-19 08:17

    You seem to be looking for the box_syntax feature, however as of Rust 1.39.0 it is not stable and only available with a nightly compiler. It also seems like this feature will not be stabilized any time soon, and might have a different design if it ever gets stabilized.

    On a nightly compiler, you can write:

    #![feature(box_syntax)]
    
    struct Test { a: i32, b: i64 }
    
    fn foo() -> Box {
        box Test { a: 123, b: 456 }
    }
    

提交回复
热议问题