帖子
分享您的知识。
Apr 01, 2025
专家问答
Why does Rust enum with Box create cycle warning even with phantom?
I'm working with Rust and I have the following code:
public enum Expr {
Apply(Box<Expr>, Box<Expr>),
}
It seems like I'm getting a cycle warning because Expr
contains Box<Expr>
. However, I'm using phantom types, and I thought that would make it safe to compile. Can someone explain why this cycle warning is happening despite using phantom types?
- Move CLI
- Move
2
1
分享
评论
答案
1CarlkawIy163
Apr 1 2025, 23:47出现循环警告是因为您的Expr
枚举定义创建了递归类型. Box<Expr>
在 Rust 中,Expr
在Box<Expr>
枚举类型内部使用会导致递归关系,因为Expr
直接引用. 这就是 Rust 发出周期警告的原因.
尽管你提到了幻像类型,但你似乎仍在为幻像类型提供具体类型,Expr``Box
而不是不需要真实实例的幻影泛型类型. 考虑一下,如果你想使用幻像类型,你可以在枚举中使用不需要在运行时存在的泛型参数,比如它Box<PhantomData<T>>``T
是一个泛型参数而不是Expr
其本身.
1
最佳答案
评论
你知道答案吗?
请登录并分享。
Move is an executable bytecode language used to implement custom transactions and smart contracts.
148帖子231答案
热门帖子