本文介绍了“添加删除检查规则时溢出"在实施fingertree时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试定义一个指状树结构并将其基本操作实现为 Rust 中的练习.我想出了以下内容,基本上是在这个 .

I'm trying to define a finger tree structure and implement its basic operations as an exercise in Rust. I've come up with the following, which is basically what's described in this paper.

use self::FingerTree::{Empty, Single, Deep};
use self::Digit::{One, Two, Three, Four};

enum Digit<A> {
    One(A),
    Two(A, A),
    Three(A, A, A),
    Four(A, A, A, A),
}

enum Node<V, A> {
    Node2(V, A, A),
    Node3(V, A, A, A),
}

enum FingerTree<V, A> {
    Empty,
    Single(A),
    Deep {
        size: V,
        prefix: Digit<A>,
        tree: Box<FingerTree<V, Node<V, A>>>,
        suffix: Digit<A>,
    },
}

fn main() {
    let e: FingerTree<i32, String> = Empty;
}

编译给了我一个我不明白的错误:

Compilation gives me an error that I don't understand:

error[E0320]: overflow while adding drop-check rules for FingerTree<i32, std::string::String>
  --> fingertree.rs:28:9
   |
28 |     let e: FingerTree<i32, String> = Empty;
   |         ^
   |
note: overflowed on enum Node variant Node2 field 0 type: i32
  --> fingertree.rs:28:9
   |
28 |     let e: FingerTree<i32, String> = Empty;
   |         ^

error[E0320]: overflow while adding drop-check rules for FingerTree<i32, std::string::String>
  --> fingertree.rs:28:38
   |
28 |     let e: FingerTree<i32, String> = Empty;
   |                                      ^^^^^
   |
note: overflowed on enum Node variant Node2 field 0 type: i32
  --> fingertree.rs:28:38
   |
28 |     let e: FingerTree<i32, String> = Empty;
   |                                      ^^^^^

为什么这不起作用?我如何使它工作?

Why is this not working? How do I make it work?

推荐答案

您创建了一个无限类型.

You have created an infinite type.

实例化 FingerTree 实例化 FingerTree 实例化 FingerTree 实例化,......而且看不到尽头.

Instantiating FingerTree<V, A> instantiates FingerTree<V, Node<V, A>> which instantiates FingerTree<V, Node<V, Node<V, A>>> which instantiates, ... and there's no end in sight.

编译器无法判断该类型实际上不会在运行时使用,因此为最坏的情况做好准备.最坏的情况是无限的.

The compiler cannot tell that the type will not actually be used at run-time, so prepares itself for the worst. And the worst is infinite.

简单地将 tree 的类型替换为 Box 可以解决问题,尽管它可能不适合当前的情况.

Simply replacing the type of tree by Box<FingerTree<V, A>> solves the issue, though it may not be correct for the situation at hand.

这篇关于“添加删除检查规则时溢出"在实施fingertree时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 23:32