gpt4 book ai didi

null - 在 Rust 函数中返回 Null

转载 作者:行者123 更新时间:2023-11-29 08:12:00 24 4
gpt4 key购买 nike

<分区>

我如何在 Rust 函数中返回 Null?

#[derive(Debug, Clone)]
struct NodeValue {
id: String,
parent: String,
children: Vec<NodeValue>,
}

impl NodeValue {
fn find_parent(&self, id: &mut String) -> &NodeValue {
if self.id == *id {
println!("{},{}", self.id, id);
return self;
}
for child in &self.children {
println!("{:?}", child);
let res = child.find_parent(id);
return res;
}

return null; //return null
}
}

fn main() {
let root = NodeValue {
id: "#".to_string(),
parent: String::from("root"),
children: vec![],
};

let id = "1";
let mut parent = "#";
let mut parent_node = root.find_parent(&mut parent);
let node1 = NodeValue {
id: "2".to_string(),
parent: "1".to_string(),
children: vec![],
};
parent_node.children.push(node1);
}

my code in playground

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com