add strings and vec

This commit is contained in:
jlav 2026-09-10 17:30:38 +02:00
parent 5e503bc46e
commit 6f22673bc8
7 changed files with 61 additions and 0 deletions

View File

@ -8,3 +8,16 @@ edition = "2024"
[[bin]]
name = "hello"
path = "hello.rs"
[[bin]]
name = "slices2"
path = "slices2.rs"
[[bin]]
name = "vec"
path = "vec.rs"
[[bin]]
name = "strings"
path = "strings.rs"

BIN
slices2 Executable file

Binary file not shown.

18
slices2.rs Normal file
View File

@ -0,0 +1,18 @@
fn main() {
let arr = [1, 2, 5];
let slice = &arr;
for i in slice {
println!("w {}", i);
}
for j in arr {
println!("ww {}", j);
}
let first = slice.get(0).unwrap();
let last = if slice.get(3).is_some() {
slice.get(3).unwrap()
} else {
&-1
};
println!("f {} l {}", first, last);
}

BIN
strings Executable file

Binary file not shown.

26
strings.rs Normal file
View File

@ -0,0 +1,26 @@
fn dump(s: &str) {
println!("{}", s);
}
fn array_to_string(arr: &[i32]) -> String {
let mut res = "[".to_string();
for v in arr {
res += &v.to_string();
res += ", "
}
res.pop();
res.push(']');
res
}
fn main() {
let txt = "test test";
let string_text = txt.to_string();
let mut string_verctor = String::new();
string_verctor.push('H');
string_verctor.push_str("hii");
dump(&string_verctor);
dump(txt);
dump(&string_text);
let arr2 = &[3, 2, 1, 0, -1];
let res = array_to_string(arr2);
}

BIN
vec Executable file

Binary file not shown.

4
vec.rs Normal file
View File

@ -0,0 +1,4 @@
fn main() {
let c: Vec<i32> = vec![];
println!("c {}", c.get(0).unwrap());
}