“在添加删除检查规则时溢出”;在实现手指树的同时 [英] "overflow while adding drop-check rules" while implementing a fingertree

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

问题描述

我正在尝试定义一个手指树结构并将其基本操作作为Rust的练习来实现。我提出了以下内容,基本上是此 paper

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< V,A> 实例化 FingerTree< V,Node< V,A> c实例化 FingerTree< V,Node< V,Node< V,A>>< 实例化...,而且没有尽头。

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.

只需将的类型替换为 Box< FingerTree< ; V,A>> 解决了该问题,尽管对于当前情况可能并不正确。

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.

这篇关于“在添加删除检查规则时溢出”;在实现手指树的同时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆