- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 Rust 的新手,为了练习,我正在构建一个简单的通用二叉树。这就是我用 C++ 创建一个的方式
template<typename T>
struct Node
{
T data;
Node<T>* parent;
Node<T>* left;
Node<T>* right;
};
template<typename T>
struct Bintree
{
Node<T>* root;
};
但是 Rust 中的相同(大概)代码似乎不起作用:
use std::ptr;
struct Node<T> {
data: T,
left: &Node<T>,
right: &Node<T>,
parent: &Node<T>,
}
struct Tree<T> {
root: &Node<T>,
}
impl Tree<T> {
pub fn new() -> Tree<T> {
Tree { root: ptr::null() }
}
pub fn insert(&self, value: T) {
if root.is_null() {
self.root = Node {
data: value,
left: ptr::null(),
right: ptr::null(),
parent: ptr::null(),
};
}
}
}
fn main() {
println!("Hello, world!");
}
这里是错误:
error[E0412]: type name `T` is undefined or not in scope
--> src/main.rs:14:15
|
14 | impl Tree<T> {
| ^ undefined or not in scope
|
= help: no candidates by the name of `T` found in your project; maybe you misspelled the name or forgot to import an external crate?
error[E0412]: type name `T` is undefined or not in scope
--> src/main.rs:15:30
|
15 | pub fn new() -> Tree<T> {
| ^ undefined or not in scope
|
= help: no candidates by the name of `T` found in your project; maybe you misspelled the name or forgot to import an external crate?
error[E0412]: type name `T` is undefined or not in scope
--> src/main.rs:19:37
|
19 | pub fn insert(&self, value: T) {
| ^ undefined or not in scope
|
= help: no candidates by the name of `T` found in your project; maybe you misspelled the name or forgot to import an external crate?
error[E0425]: unresolved name `root`. Did you mean `self.root`?
--> src/main.rs:20:16
|
20 | if root.is_null() {
| ^^^^
error[E0106]: missing lifetime specifier
--> src/main.rs:5:15
|
5 | left: &Node<T>,
| ^ expected lifetime parameter
error[E0106]: missing lifetime specifier
--> src/main.rs:6:16
|
6 | right: &Node<T>,
| ^ expected lifetime parameter
error[E0106]: missing lifetime specifier
--> src/main.rs:7:17
|
7 | parent: &Node<T>,
| ^ expected lifetime parameter
error[E0106]: missing lifetime specifier
--> src/main.rs:11:15
|
11 | root: &Node<T>,
| ^ expected lifetime parameter
我真的不明白这有什么问题。我真的不明白 Rust 的指针是如何工作的。
最佳答案
在这种情况下,你有一个基本的语法错误,应该是
impl<T> Tree<T>
从那里,您会看到您需要 if self.root.is_null()
。
然后,数据结构需要生命周期说明符,因为您正在使用引用。使用最直接的语法最终会导致
error[E0309]: the parameter type `T` may not live long enough
所以你在那里使用 T: 'a
...你最终得到:
use std::ptr;
struct Node<'a, T: 'a> {
data: T,
left: &'a Node<'a, T>,
right: &'a Node<'a, T>,
parent: &'a Node<'a, T>,
}
struct Tree<'a, T: 'a> {
root: &'a Node<'a, T>,
}
impl<'a, T> Tree<'a, T> {
pub fn new() -> Tree<'a, T> {
Tree { root: ptr::null() }
}
pub fn insert(&self, value: T) {
if self.root.is_null() {
self.root = Node {
data: value,
left: ptr::null(),
right: ptr::null(),
parent: ptr::null(),
};
}
}
}
fn main() {
println!("Hello, world!");
}
这给出了另一个错误
21 | root: ptr::null(),
| ^^^^^^^^^^^ expected reference, found *-ptr
这是因为 ptr::null()
返回原始指针,但您已声明您的数据结构使用引用。
好吧,我就到此为止了。让我们回到您的问题...
I am new to Rust, and for an exercise, I am building a simple generic binary tree.
我建议您应该考虑其他 而不是编写数据结构。它们在 Rust 中并不简单。如果您仍然想采用这种方法,我可以推荐 Too Many Lists .
关于pointers - 创建可变二叉树时键入名称 T undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40210631/
考虑代码: def foo() -> ??: return bar() 我怎么能说我希望 foo 的返回类型与 bar() 的返回类型相同? 在 C++ 中,我可以使用 decltype,类
我运行以下 mysql 查询并看到第一个查询的类型为 ALL。 mysql> EXPLAIN SELECT one.language_id as filter_id,
http://muaddibspace.blogspot.com/2008/01/type-inference-for-simply-typed-lambda.html是 Prolog 中简单类型 l
鉴于类型 type EnumerableComponentFactory = (config: { Container: React.ComponentType; Item: React.Co
我在我的自动 rmarkdown pdf 文档中使用 stargazer 包来制作漂亮的表格。默认情况下,Stargazer 将其表格放置在页面的中心。如何让观星者生成将表格与左侧对齐的 latex
class A: pass class B(A): pass ListOfA = List[A] list_of_a : ListOfA = [A(), A()] for e in [
我目前正在研究学习 WPF 和 Silverlight。到目前为止,我一直在用 Delphi 和 C# (Winforms) 进行开发。在看一些关于 WPF 的书籍和在线观看一些介绍性视频时,我的印象
在输入如下内容后: ) 按钮。但是那个按钮比较不方便。 (请注意,对于 Orientation="Horizontal" 等下拉选项不存在此问题,从下拉菜单中选择 Horizontal 后,插
我在输入以下内容时遇到问题。 问题在于 TeamIcon . 我的对象定义如下。 import TeamIcon from './components/icons/TeamIcon'; export
Demo 我这里有一个验证类是否存在于 div 中的演示。当没有类时,另一个类将从按钮中删除。基本上,当没有禁用按钮时,添加按钮上的禁用将被删除。 如果用户做的最后一件事是更改,这可以正常工作,但如果
这个问题在这里已经有了答案: How can I access object properties containing special characters? (2 个答案) 关闭 9 年前。 我
尝试创建一个 Web 应用程序(使用 mySQL 和 Python),其中包含马萨诸塞州的徒步旅行路线列表。我只想在一页上显示我的数据库中所有路径的名称,但不知道为什么什么都不显示: ########
您好,我想在使用 jQuery 的文本输入字段中键入时用点替换逗号。我现在有这段代码; $(document).on('change', '.unitprice', function() { $(
我为静态主页编写了以下代码。在这里我想使用类型化的库,它包含在部分“head.html”中。但是由于键入我的内容不断跳跃。这是因为在某个时刻字幕容器是空的。我试过添加默认占位符,但这似乎无法解决问题。
我想知道有没有类似于scanf的函数允许在c中自定义输入。我想要做的是当用户输入想要的日期时,每隔几个字符添加一个点。当用户想要确认他的输入时,他应该按下回车键,并且该变量被保存在某处。 示例:用户输
Java 中有没有办法让 ListModel 只接受特定类型?我要找的是类似 DefaultListModel 的东西或 TypedListModel ,因为 DefaultListModel 只实现
考虑如下数组类型: let example1: MyArray = ['John'], example2: MyArray = [4, 5, 1, 5, 'Eric'], exampl
我想弄清楚我是否正确输入了我的 React 高阶组件。在大多数情况下,这是正常工作的,但是在将 React 引用应用于 HOC 实例时,我遇到了输入问题。下面是一个简化的重现: import * as
这是 Python 3.7 我有一个这样的数据类: @dataclass class Action: action: str 但 Action 实际上仅限于值“bla”和“foo”。有没有一种明
我想弄清楚我是否正确输入了我的 React 高阶组件。在大多数情况下,这是正常工作的,但是在将 React 引用应用于 HOC 实例时,我遇到了输入问题。下面是一个简化的重现: import * as
我是一名优秀的程序员,十分优秀!