<String 예제_1>
fn main() {
let mut a = String::new();
a = a + "Rust on running";
println!("{}", a);
let data = "initial contents";
let s = data.to_string();
println!("{}", s);
// the method also works on a literal directlys:
let s = "finale contents".to_string();
println!("{}", s);
}
<String 예제_2>
fn main() {
let mut s = String::from("foo");
s.push_str("bar");
println!("{}", s);
let mut s1 = String::from("foo");
let s2 = "bar";
s1.push_str(&s2);
println!("s2 is {}", s2);
let mut dm = String::from("lo");
dm.push('l');
println!("{}", dm);
}
<String 예제_3>
fn main() {
// Creating a new string
let my_string = String::new();
println!("{}", my_string);
// Creating a string from a string literal
let my_string = "hello".to_string();
println!("{}", my_string);
// Concatenating two strings
let hello = "hello".to_string();
let world = "world".to_string();
let hello_world = hello + " " + &world;
println!("{}", hello_world);
// Iterating over the characters in a string
let hello = "hello".to_string();
for c in hello.chars() {
println!("{}", c);
}
// Accessing a specific character in a string
let hello = "hello".to_string();
let _c = hello.chars().nth(1);
// Splitting a string into a vector of substrings
let s = "a b c d e".to_string();
let v : Vec<&str> = s.split(' ').collect();
println!("{}", v[0]);
}
<참조 1> https://rinthel.github.io/rust-lang-book-ko/ch08-02-strings.html
<참조 2> chatGPT
'Rust' 카테고리의 다른 글
[Rust] Guess 예제 (20일차) (0) | 2023.01.01 |
---|---|
[Rust] Error 처리 Result 예제 (19일차) (0) | 2023.01.01 |
[Rust] Vector 예제 (16일차) (0) | 2022.12.27 |
[Rust] Use - Rust Programming 15일차 (0) | 2022.12.22 |
댓글