@@ -68,7 +68,7 @@ and most idiomatic way to express this logic in Rust is by defining a method nam
68
68
For our counter interface, we only need to properly increment or decrement the ` value ` of our ` Counter ` struct based on the ` Message `
69
69
we just defined:
70
70
71
- ``` rust
71
+ ``` rust,ignore
72
72
impl Counter {
73
73
fn update(&mut self, message: Message) {
74
74
match message {
@@ -85,28 +85,28 @@ impl Counter {
85
85
86
86
Great! Now we are ready to process user interactions. For instance, imagine we initialized our counter like this:
87
87
88
- ``` rust
88
+ ``` rust,ignore
89
89
let mut counter = Counter { value: 0 };
90
90
```
91
91
92
92
And let's say we wanted to simulate a user playing with our interface for a bit—pressing the increment button twice
93
93
and then the decrement button once. We could easily compute the final state of our counter with our __ update logic__ :
94
94
95
- ``` rust
95
+ ``` rust,ignore
96
96
counter.update(Message::Increment);
97
97
counter.update(Message::Increment);
98
98
counter.update(Message::Decrement);
99
99
```
100
100
101
101
This would cause our ` Counter ` to end up with a ` value ` of ` 1 ` :
102
102
103
- ``` rust
103
+ ``` rust,ignore
104
104
assert_eq!(counter.value, 1);
105
105
```
106
106
107
107
In fact, we have just written a simple test for our application logic:
108
108
109
- ``` rust
109
+ ``` rust,ignore
110
110
#[test]
111
111
fn it_counts_properly() {
112
112
let mut counter = Counter { value: 0 };
@@ -154,7 +154,7 @@ These values are normally created using a _helper function_ from the `widget` mo
154
154
155
155
For our buttons, we can use the ` button ` helper:
156
156
157
- ``` rust
157
+ ``` rust,ignore
158
158
use iced::widget::button;
159
159
160
160
let increment = button("+");
@@ -174,7 +174,7 @@ for our counter value?
174
174
While iced does not really have a ` number ` widget, it does have a more generic ` text ` widget that can be used
175
175
to display any kind of text—numbers included:
176
176
177
- ``` rust
177
+ ``` rust,ignore
178
178
use iced::widget::text;
179
179
180
180
let counter = text(15);
@@ -191,7 +191,7 @@ __six__ different ways to order them! However, the order we want is: `increment`
191
191
192
192
A very simple way of describing this order is to create a list with our widgets:
193
193
194
- ``` rust
194
+ ``` rust,ignore
195
195
let interface = vec![increment, counter, decrement];
196
196
```
197
197
@@ -205,7 +205,7 @@ manage the position of existing widgets. And since widgets are just values, they
205
205
206
206
The kind of vertical layout that we need for our counter can be achieved with the ` column ` widget:
207
207
208
- ``` rust
208
+ ``` rust,ignore
209
209
use iced::widget::column;
210
210
211
211
let interface = column![increment, counter, decrement];
@@ -226,7 +226,7 @@ In iced, every widget has a specific type that enables further configuration usi
226
226
helper returns an instance of [ the ` Button ` type] , which has an ` on_press ` method we can use to define the message it must
227
227
__ produce__ when a user presses the button:
228
228
229
- ``` rust
229
+ ``` rust,ignore
230
230
use iced::widget::button;
231
231
232
232
let increment = button("+").on_press(Message::Increment);
@@ -257,7 +257,7 @@ We are almost there! There is only one thing left to do: connecting our applicat
257
257
258
258
Let's bring together all the view logic we have written so far:
259
259
260
- ``` rust
260
+ ``` rust,ignore
261
261
use iced::widget::{button, column, text};
262
262
263
263
// The buttons
@@ -280,7 +280,7 @@ our update logic is triggered, the text widget will display the new `value`.
280
280
281
281
We can easily do this by running our view logic in a method of our ` Counter ` —just like we did with our update logic:
282
282
283
- ``` rust
283
+ ``` rust,ignore
284
284
use iced::widget::{button, column, text};
285
285
286
286
impl Counter {
@@ -310,7 +310,7 @@ Instead of throwing the `interface` away, we need to return it. Remember, the pu
310
310
to dictate the widgets of our user interface; and the content of the ` interface ` variable is precisely the
311
311
description of the interface we want:
312
312
313
- ``` rust
313
+ ``` rust,ignore
314
314
use iced::widget::{button, column, text, Column};
315
315
316
316
impl Counter {
@@ -347,7 +347,7 @@ let's just inline everything:
347
347
<img alt =" A classical counter interface " src =" resources/counter-interface.svg " width =" 50% " >
348
348
</div >
349
349
350
- ``` rust
350
+ ``` rust,ignore
351
351
use iced::widget::{button, column, text, Column};
352
352
353
353
impl Counter {
0 commit comments