@@ -40,9 +40,9 @@ En Go suele representarse el control de flujo de esta forma:
4040# func main () {
4141# x := 0
4242 if x > 0 {
43- fmt.Println (" positivo" )
43+ fmt.Println (" positivo" )
4444 } else {
45- fmt.Println (" negativo" )
45+ fmt.Println (" negativo" )
4646 }
4747# }
4848```
@@ -60,9 +60,9 @@ En Go suele representarse el control de flujo de esta forma:
6060# func main () {
6161# x := 1
6262 if x > 0 {
63- fmt.Println (" positivo" )
63+ fmt.Println (" positivo" )
6464 } else {
65- fmt.Println (" negativo" )
65+ fmt.Println (" negativo" )
6666 }
6767# }
6868```
@@ -88,26 +88,26 @@ Rust lo representaría de forma similar:
8888 <td>
8989
9090``` 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+ }
9898```
9999
100100</td >
101101 <td>
102102
103103``` 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+ }
111111```
112112
113113</td >
@@ -117,8 +117,10 @@ if x > 0 {
117117
118118Pero en Rust podemos además asignar el resultado de la expresión a una variable:
119119
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}" )
122124```
123125
124126### Switch vs Match
@@ -139,24 +141,34 @@ let mensaje = if x > 0 { "positivo" } else { "negativo" };
139141Ejemplo en Go:
140142
141143``` 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+ #}
150161```
151162
152163En Rust, el equivalente sería:
153164
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+ }
160172```
161173
162174### Bucles
@@ -171,47 +183,55 @@ match day {
171183En Go podríamos hacer lo siguiente:
172184
173185``` 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+ #}
177197```
178198
179199En Rust de forma similar podríamos conseguir el mismo efecto:
180200
181201``` rust
182- for i in 0 .. 5 {
183- println! (" {}" , i );
184- }
202+ for i in 0 .. 5 {
203+ println! (" {}" , i );
204+ }
185205```
186206
187207#### Bucle Infinito
188208
189209En Go un bucle infinito podríamos hacerlo de la siguiente forma:
190210
191- ``` go
192- for {
193- fmt.Println (" infinito" )
194- }
211+ ``` go,no_run
212+ for {
213+ fmt.Println("infinito")
214+ }
195215```
196216
197217Mientras que en Rust usaríamos:
198218
199- ``` rust,ignore
200- loop {
201- println!("infinito");
202- }
219+ ``` rust,no_run
220+ loop {
221+ println!("infinito");
222+ }
203223```
204224
205225#### Bucles con While
206226
207227Go no tiene bucle ` while ` pero en Rust si tenemos.
208228
209229``` 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+ }
215235```
216236
217237El bucle ` while ` puede resultar útil cuando buscamos una condición de corte
@@ -224,16 +244,16 @@ contadores.
224244Ambos lenguajes los tienen, pero Rust permite ` break ` con valor en ` loop ` :
225245
226246``` rust
227- let mut x = 0 ;
247+ let mut x = 0 ;
228248
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+ };
235255
236- println! (" {resultado}" );
256+ println! (" {resultado}" );
237257```
238258
239259De esta forma se asigna el valor del ` break ` dentro de ` resultado ` .
@@ -258,11 +278,11 @@ Eso no existe en Go.
258278Rust permite patrones destructurantes muy potentes que no existen en Go.
259279
260280``` rust
261- let punto = (0 , 5 );
281+ let punto = (0 , 5 );
262282
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+ }
266286```
267287
268288En 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
275295variantes, el ` match ` es exhaustivo, sino tenemos en cuenta ambos casos
276296obtendremos un error en compilación:
277297
278- ``` rust
279- let color = " azul" ;
298+ ``` rust,editable
299+ let color = "azul";
280300
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+ }
286306```
287307
0 commit comments