list

Function list 

Source
pub fn list<ID: Hash, V: View, F: Fn(&ID) -> V + 'static>(
    ids: Vec<ID>,
    f: F,
) -> List<ID, F>
Expand description

Displays a list of items all of which are represented by the same View. See examples/list.rs.

ids is a Vec of items that implement Hash.

f is a function called to generate a View for each item.

For example:

rui(list(vec![1, 2, 3], |i| {
    hstack((
        circle(),
        text(&format!("{:?}", i))
    ))
}));
Examples found in repository?
examples/any_view.rs (lines 4-11)
3fn main() {
4    rui(list(vec![7, 42], |i| {
5        if *i == 7 {
6            any_view(circle())
7        } else {
8            any_view(rectangle())
9        }
10        .padding(Auto)
11    }));
12}
More examples
Hide additional examples
examples/list.rs (lines 8-10)
3fn main() {
4    let data = vec!["John", "Paul", "George", "Ringo"];
5
6    let ids = (0usize..data.len()).collect();
7
8    rui(list(ids, move |id| {
9        hstack((circle(), data[*id].to_string()))
10    }));
11}
examples/todo_list.rs (lines 22-25)
17fn todo_list(todos: impl Binding<Vec<String>>) -> impl View {
18    with_cx(move |cx| {
19        let len = todos.with(cx, |todos| todos.len());
20        let ids = (0usize..len).collect();
21
22        list(ids, move |id| {
23            let id = *id;
24            with_cx(move |cx| todos.with(cx, |todos| todos[id].clone()))
25        })
26    })
27}
examples/counter_list.rs (lines 6-22)
3fn main() {
4    rui(state(Counters::default, |counters, cx| {
5        vstack((
6            list(cx[counters].ids(), move |&i| {
7                with_cx(move |cx| {
8                    let count = bind(counters, CounterLens(i));
9                    hstack((
10                        format!("{}", count.get(cx)).padding(Auto),
11                        button("increment", move |cx| {
12                            *count.get_mut(cx) += 1;
13                        })
14                        .padding(Auto),
15                        button("decrement", move |cx| {
16                            *count.get_mut(cx) -= 1;
17                        })
18                        .padding(Auto),
19                        button("remove", move |cx| cx[counters].remove_counter(i)).padding(Auto),
20                    ))
21                })
22            }),
23            format!("total: {}", cx[counters].sum_counters()).padding(Auto),
24            button("add counter", move |cx| cx[counters].add_counter()).padding(Auto),
25        ))
26    }));
27}