feat: strings

This commit is contained in:
Ahmet Kaan GÜMÜŞ 2024-07-20 22:38:29 +03:00
parent deda0d8532
commit 5c1e8698fa
2 changed files with 55 additions and 0 deletions

6
22-strings/Cargo.toml Normal file
View file

@ -0,0 +1,6 @@
[package]
name = "strings"
version = "0.1.0"
edition = "2021"
[dependencies]

49
22-strings/src/main.rs Normal file
View file

@ -0,0 +1,49 @@
fn main() {
println!("Hello, world!");
// This is how we can create a string.
let empty_string = String::new();
println!("{}", empty_string);
// This is how we can create string with initial value
let mut how_are_you = String::from("Hi");
// This appends string to the string
how_are_you.push_str(", How are you ");
// This appends a char to the string
how_are_you.push('?');
println!("{}", how_are_you);
// to_string is also equivalent for String::from
let drink = "Drink".to_string();
let water = "Water".to_string();
let drink_water = drink + &water;
println!("{}", drink_water);
// This won't work since '+' operation takes ownership of first
// println!("{}", drink);
// But this works
println!("{}", water);
let stay = "stay".to_string();
let safe = "safe".to_string();
// This all going to work because format doesn't take ownerships
let stay_safe = format!("{}{}", stay, safe);
println!("{}", stay_safe);
println!("{}", stay);
println!("{}", safe);
// This doesn't work because string indexes can't be reached like this.
// Because non ascii characters may take more than one slot.
// Rust prevents potential errors in the first place
// println!("{}", stay_safe[0]);
for char in stay_safe.chars() {
println!("{}", char);
}
for byte in stay_safe.bytes() {
println!("{}", byte);
}
}