简介:我是 Rust 新手,所以我决定通过实现双链表来练习。出于调试目的,我实现了该get()
方法,但未能从Rc<RefCell<_>>
. (抱歉问了个愚蠢的问题)
问题:我试图返回一个Result<T, &'static str>
in ,.get()
其中T
是节点中存储的数据的类型,&str
是错误消息字符串。借用检查器告诉我,我无法返回对方法内变量的引用,因此我尝试将内部值复制出来并返回它,但失败了。
源代码:
use std::{rc::Rc, cell::RefCell};
struct Node<T> {
data: Option<T>,
prev: Option<Rc<RefCell<Node<T>>>>,
next: Option<Rc<RefCell<Node<T>>>>,
}
impl<T> Node<T> {
/// Instantiate a new dummy node.
/// This node is used to mark the start and end of the list.
/// It is not counted in the size of the list.
fn new() -> Self {
Node {
data: None, …
Run Code Online (Sandbox Code Playgroud)