add strings and vec
This commit is contained in:
parent
5e503bc46e
commit
6f22673bc8
13
Cargo.toml
13
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"
|
||||
|
||||
18
slices2.rs
Normal file
18
slices2.rs
Normal 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);
|
||||
}
|
||||
26
strings.rs
Normal file
26
strings.rs
Normal 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);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user