cadenas y arrays

This commit is contained in:
2023-05-25 11:00:27 +02:00
parent c950c6f286
commit 24a9144720
7 changed files with 78 additions and 1 deletions

View File

@@ -1,4 +1,5 @@
https://tinchicus.com/2022/09/26/rust-listado-del-curso-inicial/
- https://tinchicus.com/2022/06/01/rust-usando-a-cargo/ (hola_mundo)
- https://tinchicus.com/2022/06/03/rust-strings/
- https://tinchicus.com/2022/06/03/rust-strings/ (cadenas)
- https://tinchicus.com/2022/06/07/rust-vector/ (arreglos)

7
arreglos/Cargo.lock generated Normal file
View File

@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "arreglos"
version = "0.1.0"

8
arreglos/Cargo.toml Normal file
View File

@@ -0,0 +1,8 @@
[package]
name = "arreglos"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

21
arreglos/src/main.rs Normal file
View File

@@ -0,0 +1,21 @@
fn main() {
// definimos un array
let mi_arreglo_3 = ["Lovecraft", "Poe", "Barker", "King"];
// array mutable con 4 enteros
let mut _mi_arreglo_2: [i32; 4] = [2, 4, 8, 16];
// arreglo vacío tipo string
let mut _arreglo_vacio: [&str; 0] = [];
// crea el array con cuatro veces el 123
let arreglo = [123; 4];
// imprimimos para que no den error
println!("{} {} {} ", mi_arreglo_3[1], _mi_arreglo_2[2], arreglo[3]);
let mut mi_arreglo: [&str; 4] = ["", "", "", ""];
mi_arreglo[0] = "Lovecraft";
mi_arreglo[1] = "Poe";
mi_arreglo[2] = "Barker";
mi_arreglo[3] = "King";
println!("{}, {}, {}, {}", mi_arreglo[0], mi_arreglo[1], mi_arreglo[2], mi_arreglo[3]);
}

7
cadenas/Cargo.lock generated Normal file
View File

@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "cadenas"
version = "0.1.0"

8
cadenas/Cargo.toml Normal file
View File

@@ -0,0 +1,8 @@
[package]
name = "cadenas"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

25
cadenas/src/main.rs Normal file
View File

@@ -0,0 +1,25 @@
fn main() {
string1();
string2();
}
fn string1() {
let cadena1 = "Tinchicus.com";
let cadena2 = " es el ";
let cadena3 = "mejor lugar para aprender";
// utilizamos a to_owned para convertir este segmento en una string y podamos concatenar los otros segmentos a este
let _renglon = cadena1.to_owned() + cadena2 + cadena3;
println!("{} {}", _renglon, cadena3)
}
fn string2() {
let mut renglon_final = String::new();
let cadena1 = "Tinchicus.com";
let cadena2 = " es el ";
let cadena3 = "mejor lugar para aprender";
renglon_final = format!("{}{}{}", cadena1, cadena2, cadena3);
println!("{}", renglon_final);
}