Merge pull request #260 from wgdzlh/fix-string-array-initializing

fix: use map func to avoid copy of string
This commit is contained in:
Sunface 2022-07-07 08:38:36 +08:00 committed by GitHub
commit 98883b8279
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 3 additions and 3 deletions

View File

@ -127,7 +127,7 @@ fn main() {
check_size([0u8; 767]);
check_size([0i32; 191]);
check_size(["hello你好"; __]); // Size of &str ?
check_size(["hello你好".to_string(); __]); // Size of String?
check_size([(); __].map(|_| "hello你好".to_string())); // Size of String?
check_size(['中'; __]); // Size of char ?
println!("Success!");

View File

@ -53,7 +53,7 @@ fn main() {
check_size([0u8; 767]);
check_size([0i32; 191]);
check_size(["hello你好"; 47]); // &str is a string reference, containing a pointer and string length in it, so it takes two word long, in x86-64, 1 word = 8 bytes
check_size(["hello你好".to_string(); 31]); // String is a smart pointer struct, it has three fields: pointer, length and capacity, each takes 8 bytes
check_size([(); 31].map(|_| "hello你好".to_string())); // String is a smart pointer struct, it has three fields: pointer, length and capacity, each takes 8 bytes
check_size(['中'; 191]); // A char takes 4 bytes in Rust
}

View File

@ -123,7 +123,7 @@ fn main() {
check_size([0u8; 767]);
check_size([0i32; 191]);
check_size(["hello你好"; __]); // size of &str ?
check_size(["hello你好".to_string(); __]); // size of String?
check_size([(); __].map(|_| "hello你好".to_string())); // size of String?
check_size(['中'; __]); // size of char ?
}