From 20e0dccffe73150a6068cc3731e8dffa49b0fa65 Mon Sep 17 00:00:00 2001 From: wgdzlh <13d1101@gmail.com> Date: Tue, 5 Jul 2022 18:08:24 +0800 Subject: [PATCH 1/2] fix: use map func to avoid copy of string --- solutions/generics-traits/const-generics.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/generics-traits/const-generics.md b/solutions/generics-traits/const-generics.md index e1f491e..b0a090e 100644 --- a/solutions/generics-traits/const-generics.md +++ b/solutions/generics-traits/const-generics.md @@ -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 } From 1d510fadc92e4522a443af5ca362449d55cce37c Mon Sep 17 00:00:00 2001 From: wgdzlh <13d1101@gmail.com> Date: Tue, 5 Jul 2022 18:08:24 +0800 Subject: [PATCH 2/2] fix: use map func to avoid copy of string --- en/src/generics-traits/const-generics.md | 2 +- solutions/generics-traits/const-generics.md | 2 +- zh-CN/src/generics-traits/const-generics.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/en/src/generics-traits/const-generics.md b/en/src/generics-traits/const-generics.md index 660cee5..d3217ed 100644 --- a/en/src/generics-traits/const-generics.md +++ b/en/src/generics-traits/const-generics.md @@ -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!"); diff --git a/solutions/generics-traits/const-generics.md b/solutions/generics-traits/const-generics.md index e1f491e..b0a090e 100644 --- a/solutions/generics-traits/const-generics.md +++ b/solutions/generics-traits/const-generics.md @@ -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 } diff --git a/zh-CN/src/generics-traits/const-generics.md b/zh-CN/src/generics-traits/const-generics.md index 6695cdd..f2ebe97 100644 --- a/zh-CN/src/generics-traits/const-generics.md +++ b/zh-CN/src/generics-traits/const-generics.md @@ -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 ? }