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

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

In this code...

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


        
4条回答
  •  忘掉有多难
    2020-12-19 08:23

    Is there a way to allocate directly to the heap without box?

    No. If there was, it wouldn't need a language change.

    People tend to avoid this by using the unstable syntax indirectly, such as by using one of the standard containers which, in turn, uses it internally.

    See also:

    • How to allocate arrays on the heap in Rust 1.0?
    • Is there any way to allocate a standard Rust array directly on the heap, skipping the stack entirely?
    • What does the box keyword do?
    • What is the <- symbol in Rust?

提交回复
热议问题