hi everyone, I'm trying to learn rust on my free days.I am a js developer.In rust I'm trying to create a component based UI template.This is the minimum example I can reproduce in Rust playground.
In javascript, this can be created easily, but in rust, many things are different.
I already know that rust doesn't allow you to do certain things. You have to organize the code in a different way.
I have a vector of enums. I also want to add components that will return a new set of vectors.The component returns a vector from a member function that is not a reference.
let _new_children = match new_view.unwrap() {
View::View(children) => children, // reference &Vec<View>
View::Render(ref component) => component.render(), // struct Vec<View>
};
let _new_children = match new_view.unwrap() {
View::View(children) => children,
View::Render(ref component) => &component.render(), // temporary value dropped while borrowed
};
How can I solve this problem, do I need to rewrite the way functions check the difference between two vectors (itertools has a zip_longest method, which I also use)
Hey,
I'm still somewhat of a beginner, but I think I can help you with this one.
Your problem is, that &component.render() creates a new Vec but that newly created vector only lives inside the match block.
That's why the compiler is telling you that "the temporary value is dropped".
So for example here:
let _new_children = match new_view.unwrap() {
View::View(children) => children,
View::Render(ref component) => &component.render(), // temporary value dropped while borrowed
};
you are trying to make _new children store a reference (&) to the Vec created by component.render(). But this result only lives inside the second match arm ( View::Render(ref component), so you can not create a reference outside of the match block, because the new vector would already be gone.
I wrote a working version of your code using highly nested match blocks to illustrate what you could do.
I really hope this somewhat clarifies your problem.