Move.

帖子

分享您的知识。

Santorini.
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
分享
评论
.

答案

1
CarlkawIy.
Apr 1 2025, 23:47

出现循环警告是因为您的Expr枚举定义创建了递归类型. Box<Expr>在 Rust 中,ExprBox<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答案
Sui.X.Peera.

赚取你的 1000 Sui 份额

获取声誉积分,并因帮助 Sui 社区成长而获得奖励。

我们使用 cookie 确保您在我们的网站上获得最佳体验。
更多信息