Given the code
struct NodeFragment{
char: String,
name: String,
cost: i32,
r#type: String
}
pub struct Node {
pub char_name: String,
pub node_name: String,
pub cp_cost: i32,
pub node_type: NodeType,
}
impl From<Node> for NodeFragment{
fn from(value: Node) -> Self {
NodeFragment{ char: value.char_name, name: value.node_name, cost: value.cp_cost, r#type: value.node_type.to_string() }
}
}
impl From<Vec<Node>> for NodeFragment{
fn from(value: Vec<Node>) -> Vec<NodeFragment> {
//foreach mapping value to Vec<NodeFragment>
vec![]
}
}
I get that error:
method `from` has an incompatible type for trait
expected signature `fn(Vec<_>) -> NodeFragment`
found signature `fn(Vec<_>) -> Vec<NodeFragment>`rustcClick for full compiler diagnostic
main.rs(34, 34): change the output type to match the trait: `NodeFragment`
How do i implement the method from for a Vec<Node>
to get a Vec<NodeFragment>
?
The concept for the from Trait i assume is, that i convert Type A to Type B. The error message above, doesn’t give me any details. Is it not allowed to implement from for Vec Type outside of the Vec crate?
New contributor
RiseYx Saramoso is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.