目前,我的节点是由一个包含名称和值的结构组成的。
稍后,图形被创建。
我想要更改某些节点的值。我也希望结构在某一时刻能容纳许多其他类别。
问题是,在将节点中的节点放在图中后,在中无法更改结构的值。
在下面的代码中,我有3个节点,其中两个连接到一个主节点。我找到主节点的子节点,然后对每个子节点,我尝试将其结构的值更改为任意值(用于测试)。
use petgraph::stable_graph::StableGraph;
#[derive(Clone, Debug, Default)]
struct ControlBloc {
name: String,
value: u32,
}
#[derive(Clone, PartialEq, PartialOrd, Debug, Default)]
struct Arcs {
speed_arcs: f32,
bandwidth: f32,
state_bandwidth: f32,
}
fn create_bloc(name: String, value: u32) -> ControlBloc {
ControlBloc { name, value }
}
fn main() {
let mut graph = StableGraph::<&ControlBloc, &Arcs>::new();
let m = &create_bloc(String::from("Main"), 10);
let b1 = &create_bloc(String::from("sub1"), 20);
let b2 = &create_bloc(String::from("sub2"), 30);
let main = graph.add_node(m);
let sub1 = graph.add_node(b1);
let sub2 = graph.add_node(b2);
graph.add_edge(
main,
sub1,
&Arcs {
speed_arcs: 1.0,
bandwidth: 1.02,
state_bandwidth: 0.0,
},
);
graph.add_edge(
main,
sub2,
&Arcs {
speed_arcs: 2.0,
bandwidth: 2.02,
state_bandwidth: 0.0,
},
);
let neighb = graph.neighbors(main).collect::<Vec<_>>();
for i in 0..neighb.len() {
let &mut nod = graph.node_weight_mut(neighb[i]).unwrap();
let ar = *graph
.edge_weight(graph.find_edge(main, neighb[i]).unwrap())
.unwrap();
println!("{:?}", nod);
nod.value = 10; // ########## Problem Here :( ################
println!("{:?}", nod);
println!("{:?}", ar);
}
}无论我想做什么,我都会犯一个错误:
error[E0594]: cannot assign to `nod.value`, which is behind a `&` reference
--> src/main.rs:56:9
|
51 | let &mut nod = graph.node_weight_mut(neighb[i]).unwrap();
| --- consider changing this binding's type to be: `&mut ControlBloc`
...
56 | nod.value = 10; // ########## Problem Here :( ################
| ^^^^^^^^^^^^^^ `nod` is a `&` reference, so the data it refers to cannot be written它是否告诉我,一旦在图中设置了值,就不可能改变它们?因为如果我想打印它们,那很好,但是如果我不能对它们做任何事情的话,那就有点没用了。
我可以想到的一个解决方案是创建一个具有我想要更改的值的节点的副本,当我创建它时,我会将正确的值放入,然后通过替换图中的边来用新节点替换旧节点。但是,如果有一种方法来修复原始代码,这似乎是一个相当长的解决方案。
发布于 2022-07-15 07:14:07
您的问题是,您的图并不是拥有值,而是只保存对它们的引用。
您不能操作引用,您应该持有一个&mut (这通常是有问题的),或者将所有权转移到图中。
这是我的代码,它起作用了:
fn main() {
let mut graph = StableGraph::<ControlBloc, Arcs>::new();
let m = create_bloc(String::from("Main"), 10);
let b1 = create_bloc(String::from("sub1"), 20);
let b2 = create_bloc(String::from("sub2"), 30);
let main = graph.add_node(m);
let sub1 = graph.add_node(b1);
let sub2 = graph.add_node(b2);
graph.add_edge(
main,
sub1,
Arcs {
speed_arcs: 1.0,
bandwidth: 1.02,
state_bandwidth: 0.0,
},
);
graph.add_edge(
main,
sub2,
Arcs {
speed_arcs: 2.0,
bandwidth: 2.02,
state_bandwidth: 0.0,
},
);
let neighb = graph.neighbors(main).collect::<Vec<_>>();
for i in 0..neighb.len() {
let nod = graph.node_weight_mut(neighb[i]).unwrap();
println!("{:?}", nod);
nod.value = 10;
println!("{:?}", nod);
let ar = graph
.edge_weight(graph.find_edge(main, neighb[i]).unwrap())
.unwrap();
println!("{:?}", ar);
}
}请不要让我如何将StableGraph::<&ControlBloc, &Arcs>::new()更改为StableGraph::<ControlBloc, Arcs>::new(),因为Rust可以推断出这些类型,这可以被最终缩短为StableGraph::new()。
还请注意,我将let ar = ...代码向下移动,因为否则您将得到另一个借入错误。
https://stackoverflow.com/questions/72983084
复制相似问题