fizz_buzz/
fizz_buzz.rs

1#![feature(type_alias_impl_trait, impl_trait_in_assoc_type)]
2
3use nuit::{Text, HStack, VStack, View, Bind, Button, State, If, clone};
4
5#[derive(Bind)]
6struct FizzBuzzView {
7    count: State<i32>,
8}
9
10impl FizzBuzzView {
11    fn new() -> Self {
12        Self { count: State::new(1) }
13    }
14}
15
16impl View for FizzBuzzView {
17    type Body = impl View;
18
19    fn body(&self) -> Self::Body {
20        let count = self.count.clone();
21        let fizz = count.get() % 3 == 0;
22        let buzz = count.get() % 5 == 0;
23        VStack::new((
24            Button::with_text("Increment", clone!(count => move || {
25                count.set(count.get() + 1);
26            })),
27            If::new_or_else(fizz || buzz, || {
28                HStack::new((
29                    If::new(fizz, || Text::new("Fizz")),
30                    If::new(buzz, || Text::new("Buzz")),
31                ))
32            }, || {
33                Text::new(format!("{}", self.count.get()))
34            }),
35        ))
36    }
37}
38
39fn main() {
40    nuit::run_app(FizzBuzzView::new());
41}