@@ -40,9 +40,9 @@ En Go suele representarse el control de flujo de esta forma:
40
40
# func main () {
41
41
# x := 0
42
42
if x > 0 {
43
- fmt.Println (" positivo" )
43
+ fmt.Println (" positivo" )
44
44
} else {
45
- fmt.Println (" negativo" )
45
+ fmt.Println (" negativo" )
46
46
}
47
47
# }
48
48
```
@@ -60,9 +60,9 @@ En Go suele representarse el control de flujo de esta forma:
60
60
# func main () {
61
61
# x := 1
62
62
if x > 0 {
63
- fmt.Println (" positivo" )
63
+ fmt.Println (" positivo" )
64
64
} else {
65
- fmt.Println (" negativo" )
65
+ fmt.Println (" negativo" )
66
66
}
67
67
# }
68
68
```
@@ -88,26 +88,26 @@ Rust lo representaría de forma similar:
88
88
<td>
89
89
90
90
``` rust
91
- # let x = 0 ;
92
- #
93
- if x > 0 {
94
- println! (" positivo" );
95
- } else {
96
- println! (" negativo" );
97
- }
91
+ # let x = 0 ;
92
+ #
93
+ if x > 0 {
94
+ println! (" positivo" );
95
+ } else {
96
+ println! (" negativo" );
97
+ }
98
98
```
99
99
100
100
</td >
101
101
<td>
102
102
103
103
``` rust
104
- # let x = 1 ;
105
- #
106
- if x > 0 {
107
- println! (" positivo" );
108
- } else {
109
- println! (" negativo" );
110
- }
104
+ # let x = 1 ;
105
+ #
106
+ if x > 0 {
107
+ println! (" positivo" );
108
+ } else {
109
+ println! (" negativo" );
110
+ }
111
111
```
112
112
113
113
</td >
@@ -117,8 +117,10 @@ if x > 0 {
117
117
118
118
Pero en Rust podemos además asignar el resultado de la expresión a una variable:
119
119
120
- ``` rust,ignore
121
- let mensaje = if x > 0 { "positivo" } else { "negativo" };
120
+ ``` rust
121
+ #let x = 0 ;
122
+ let mensaje = if x > 0 { " positivo" } else { " negativo" };
123
+ #println! (" {mensaje}" )
122
124
```
123
125
124
126
### Switch vs Match
@@ -139,24 +141,34 @@ let mensaje = if x > 0 { "positivo" } else { "negativo" };
139
141
Ejemplo en Go:
140
142
141
143
``` go
142
- switch day {
143
- case " lunes" :
144
- fmt.Println (" Inicio de semana" )
145
- case " viernes" :
146
- fmt.Println (" Casi fin" )
147
- default :
148
- fmt.Println (" Otro día" )
149
- }
144
+ #package main
145
+ #
146
+ #import (
147
+ # " fmt"
148
+ #)
149
+ #
150
+ #func main () {
151
+ # day := " lunes"
152
+ switch day {
153
+ case " lunes" :
154
+ fmt.Println (" Inicio de semana" )
155
+ case " viernes" :
156
+ fmt.Println (" Casi fin" )
157
+ default :
158
+ fmt.Println (" Otro día" )
159
+ }
160
+ #}
150
161
```
151
162
152
163
En Rust, el equivalente sería:
153
164
154
- ``` rust,ignore
155
- match day {
156
- "lunes" => println!("Inicio de semana"),
157
- "viernes" => println!("Casi fin"),
158
- _ => println!("Otro día"),
159
- }
165
+ ``` rust
166
+ # let day = " lunes" ;
167
+ match day {
168
+ " lunes" => println! (" Inicio de semana" ),
169
+ " viernes" => println! (" Casi fin" ),
170
+ _ => println! (" Otro día" ),
171
+ }
160
172
```
161
173
162
174
### Bucles
@@ -171,47 +183,55 @@ match day {
171
183
En Go podríamos hacer lo siguiente:
172
184
173
185
``` go
174
- for i := 0 ; i < 5 ; i++ {
175
- fmt.Println (i)
176
- }
186
+ #package main
187
+ #
188
+ #import (
189
+ # " fmt"
190
+ #)
191
+ #
192
+ #func main () {
193
+ for i := 0 ; i < 5 ; i++ {
194
+ fmt.Println (i)
195
+ }
196
+ #}
177
197
```
178
198
179
199
En Rust de forma similar podríamos conseguir el mismo efecto:
180
200
181
201
``` rust
182
- for i in 0 .. 5 {
183
- println! (" {}" , i );
184
- }
202
+ for i in 0 .. 5 {
203
+ println! (" {}" , i );
204
+ }
185
205
```
186
206
187
207
#### Bucle Infinito
188
208
189
209
En Go un bucle infinito podríamos hacerlo de la siguiente forma:
190
210
191
- ``` go
192
- for {
193
- fmt.Println (" infinito" )
194
- }
211
+ ``` go,no_run
212
+ for {
213
+ fmt.Println("infinito")
214
+ }
195
215
```
196
216
197
217
Mientras que en Rust usaríamos:
198
218
199
- ``` rust,ignore
200
- loop {
201
- println!("infinito");
202
- }
219
+ ``` rust,no_run
220
+ loop {
221
+ println!("infinito");
222
+ }
203
223
```
204
224
205
225
#### Bucles con While
206
226
207
227
Go no tiene bucle ` while ` pero en Rust si tenemos.
208
228
209
229
``` rust
210
- let mut i = 0 ;
211
- while i < 5 {
212
- println! (" {}" , i );
213
- i += 1 ;
214
- }
230
+ let mut i = 0 ;
231
+ while i < 5 {
232
+ println! (" {}" , i );
233
+ i += 1 ;
234
+ }
215
235
```
216
236
217
237
El bucle ` while ` puede resultar útil cuando buscamos una condición de corte
@@ -224,16 +244,16 @@ contadores.
224
244
Ambos lenguajes los tienen, pero Rust permite ` break ` con valor en ` loop ` :
225
245
226
246
``` rust
227
- let mut x = 0 ;
247
+ let mut x = 0 ;
228
248
229
- let resultado = loop {
230
- if x > 10 {
231
- break x * 2 ; // aquí
232
- }
233
- x += 1 ;
234
- };
249
+ let resultado = loop {
250
+ if x > 10 {
251
+ break x * 2 ; // aquí
252
+ }
253
+ x += 1 ;
254
+ };
235
255
236
- println! (" {resultado}" );
256
+ println! (" {resultado}" );
237
257
```
238
258
239
259
De esta forma se asigna el valor del ` break ` dentro de ` resultado ` .
@@ -258,11 +278,11 @@ Eso no existe en Go.
258
278
Rust permite patrones destructurantes muy potentes que no existen en Go.
259
279
260
280
``` rust
261
- let punto = (0 , 5 );
281
+ let punto = (0 , 5 );
262
282
263
- if let (0 , y ) = punto {
264
- println! (" Está sobre el eje Y en {y}" );
265
- }
283
+ if let (0 , y ) = punto {
284
+ println! (" Está sobre el eje Y en {y}" );
285
+ }
266
286
```
267
287
268
288
En este caso si ` opcion ` es equivalente a la variante ` Some ` , se extrae el valor
@@ -275,13 +295,13 @@ Con `match` podríamos hacer algo similar, pero en este caso debemos cubrir amba
275
295
variantes, el ` match ` es exhaustivo, sino tenemos en cuenta ambos casos
276
296
obtendremos un error en compilación:
277
297
278
- ``` rust
279
- let color = " azul" ;
298
+ ``` rust,editable
299
+ let color = "azul";
280
300
281
- match color {
282
- " rojo" => println! (" El color es rojo" ),
283
- " verde" => println! (" El color es verde" ),
284
- otro => println! (" Otro color, el cual es {otro}" ),
285
- }
301
+ match color {
302
+ "rojo" => println!("El color es rojo"),
303
+ "verde" => println!("El color es verde"),
304
+ otro => println!("Otro color, el cual es {otro}"),
305
+ }
286
306
```
287
307
0 commit comments