For statements with keys
If the list often changes, it's better to specify a key for the list.
The key should be an unique identifier for each item. It is used to identify how the list changes and make list changes faster.
To specify a key, the list structure must implement "AsListKey".
#[component(Backend = DomBackend)]
struct MyWebsite {
template: template! {
for item in &self.my_list use u32 {
<div> { &item.s } </div>
}
},
my_list: Vec<MyListItem>,
}
struct MyListItem {
id: u32,
s: String,
}
impl AsListKey for MyListItem {
type ListKey = u32;
fn as_list_key(&self) -> &u32 {
&self.id
}
}
If the for statement generates more than one variable, it is required to specify which variable is used as the key.
#[component(Backend = DomBackend)]
struct MyWebsite {
template: template! {
for (_index, item) in self.my_list.iter().enumerate()
use (item) u32
{
<div> { &item.s } </div>
}
},
my_list: Vec<MyListItem>,
}