diff --git a/Cargo.toml b/Cargo.toml index c7da91e..ecdcfc3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/slices2 b/slices2 new file mode 100755 index 0000000..7da4f5c Binary files /dev/null and b/slices2 differ diff --git a/slices2.rs b/slices2.rs new file mode 100644 index 0000000..3ff24e0 --- /dev/null +++ b/slices2.rs @@ -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); +} diff --git a/strings b/strings new file mode 100755 index 0000000..832d050 Binary files /dev/null and b/strings differ diff --git a/strings.rs b/strings.rs new file mode 100644 index 0000000..2d314ff --- /dev/null +++ b/strings.rs @@ -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); +} diff --git a/vec b/vec new file mode 100755 index 0000000..c934a9e Binary files /dev/null and b/vec differ diff --git a/vec.rs b/vec.rs new file mode 100644 index 0000000..bf54738 --- /dev/null +++ b/vec.rs @@ -0,0 +1,4 @@ +fn main() { + let c: Vec = vec![]; + println!("c {}", c.get(0).unwrap()); +}