-
Rust) What are the differences between String and str?차이점String vs str코딩Coding/★Rust★기초★Syntax 2022. 4. 13. 10:32728x90
Easy Rust 한글 강의
프로그래밍 언어 러스트를 배웁시다! 015 Easy Rust in Korean: String methods
1 fn main() { 2 let mut my_name = String::with_capacity(26); 3 println!( 4 "Length is {} and capacity is : {}", 5 my_name.len(), 6 my_name.capacity() 7 ); 8 9 my_name.push_str("David!"); 10 println!( 11 "Length is {} and capacity is : {}", 12 my_name.len(), 13 my_name.capacity() 14 ); 15 16 my_name.push_str(" and I live in Seoul"); 17 println!( 18 "Length is {} and capacity is : {}", 19 my_name.len(), 20 my_name.capacity() 21 ); 22 23 my_name.push('a'); 24 println!( 25 "Length is {} and capacity is : {}", 26 my_name.len(), 27 my_name.capacity() 28 ); 29 } 30
fn main() { let mut my_name = String::with_capacity(26); println!( "Length is {} and capacity is : {}", my_name.len(), my_name.capacity() ); my_name.push_str("David!"); println!( "Length is {} and capacity is : {}", my_name.len(), my_name.capacity() ); my_name.push_str(" and I live in Seoul"); println!( "Length is {} and capacity is : {}", my_name.len(), my_name.capacity() ); my_name.push('a'); println!( "Length is {} and capacity is : {}", my_name.len(), my_name.capacity() ); }
결과
Compiling training_rustacean_rust v0.1.0 (/Users/globalyoung/Documents/Project/Github/rust_project/training_rustacean_rust)
Finished dev [unoptimized + debuginfo] target(s) in 0.33s
Running `target/debug/training_rustacean_rust`
Length is 0 and capacity is : 26
Length is 6 and capacity is : 26
Length is 26 and capacity is : 26
Length is 27 and capacity is : 52Why does Rust have String and str? What are the differences between String and str? When does one use String instead of str and vice versa? Is one of them getting deprecated?
https://stackoverflow.com/questions/24158114/what-are-the-differences-between-rusts-string-and-str
https://www.ameyalokare.com/rust/2017/10/12/rust-str-vs-String.html
Why does Rust have different types of Strings? Why not just one String type?"
https://dev.to/stevepryde/rust-string-vs-str-1l93반응형'코딩Coding > ★Rust★기초★Syntax' 카테고리의 다른 글
Consumes and leaks the Box, returning a mutable reference, &'a mut T. (0) 2022.04.17 Rust's Rules Are Made to Be Broken(warp blog) (0) 2022.04.16 Rust]유니코드 출력하는 방법 Unicode print (0) 2022.04.14 rust 비교 연산test == != string 비교하기eq, ne (0) 2022.04.14 Rust - String 기초 example 있음 (0) 2022.04.13 3 real-world visitor design pattern examples in Rust, JS, & C++ (0) 2022.04.13 PDXRust September 2016: Nick Cameron - Design Patterns in Rust (0) 2022.04.13 Rust-Error Handling - FP Complete Cor. (0) 2022.04.12