这是代码
pub struct List<'a> {
node: Option<Node<'a>>,
list_vec: Vec<Node<'a>>,
}
pub struct Node<'a> {
data: String,
next: Option<&'a Node<'a>>,
}
impl List<'_> {
fn add(&mut self, val: String, referenced_node: &Node){
self.node = Some(Node {
data: String::from(String::from(val)),
next: Some(referenced_node), // I was expecting to have reference to original variable which i transferred via new_node
});
}
我期望存储对原始变量的引用,正如我在代码注释中所说的那样
将生命周期
'a
与您放入 List
中的引用相关联:
// v v
impl<'a> List<'a> {
// v
fn add(&mut self, val: String, referenced_node: &'a Node) {
self.node = Some(Node {
data: String::from(String::from(val)),
next: Some(referenced_node), // I was expecting to have reference to original variable which i transferred via new_node
});
}
}