compound-types/slice #187
Replies: 34 comments 10 replies
-
第二题的答案中先使用std::mem::size_of函数计算usize类型占用的字节数会不会好一些? |
Beta Was this translation helpful? Give feedback.
-
第二题的注释有问题:”'中'和'国'是 UTF-8 字符,它们每个占用 3 个字节“,但是char类型是Unicode编码,大小固定为4字节,两个字符应该是8字节。 |
Beta Was this translation helpful? Give feedback.
-
https://github.com/sunface/rust-by-practice/blob/master/solutions/compound-types/slice.md |
Beta Was this translation helpful? Give feedback.
-
第二题为什么是16🤔🤨 |
Beta Was this translation helpful? Give feedback.
-
我怎么觉得,是不是第二题的题干部分的内容跟assert没啥关系。。就是我无论上面写的长度多大,取得内容有多长,在字节引用上就单纯的跟系统有关跟内容无关? |
Beta Was this translation helpful? Give feedback.
-
第 6 题的话把返回值改成 String 也行, 这样 word 的所有权就在自己手上了 |
Beta Was this translation helpful? Give feedback.
-
第二题注释有点问题:字符串是UTF-8变长编码,字符应该是Unicode类型,占4个字节 |
Beta Was this translation helpful? Give feedback.
-
2022.11.11 Done |
Beta Was this translation helpful? Give feedback.
-
第二题:一个切片引用就是 16 个字节大小 和 内容大小无关。 |
Beta Was this translation helpful? Give feedback.
-
第6题的个人思路:把两行代码换个顺序就行。因为ch是对s的一个不可变引用,而调用clear时又会创建一个可变引用(你去看clear的源码,它的参数是&mut self),所以如果先clear的话,会导致可变引用与不可变引用同时存在,无法过编译。 // 修复所有错误
fn main() {
let mut s = String::from("hello world");
// 这里, &s 是 `&String` 类型,但是 `first_character` 函数需要的是 `&str` 类型。
// 尽管两个类型不一样,但是代码仍然可以工作,原因是 `&String` 会被隐式地转换成 `&str` 类型,如果大家想要知道更多,可以看看 Deref 章节: https://course.rs/advance/smart-pointer/deref.html
let ch = first_character(&s);
println!("the first character is: {}", ch);
s.clear(); // error!
}
fn first_character(s: &str) -> &str {
&s[..1]
} |
Beta Was this translation helpful? Give feedback.
-
done lala~ |
Beta Was this translation helpful? Give feedback.
-
done--2023/12/10 |
Beta Was this translation helpful? Give feedback.
-
good rust prob |
Beta Was this translation helpful? Give feedback.
-
mark finished |
Beta Was this translation helpful? Give feedback.
-
函数std::mem::size_of_val的签名:pub const fn size_of_val(val: &T) -> usize |
Beta Was this translation helpful? Give feedback.
-
damn , nice one |
Beta Was this translation helpful? Give feedback.
-
第一题的描述感觉有点不太清楚, 建议给 as str ---> // as str 加上注释, 会有助于理解题意 |
Beta Was this translation helpful? Give feedback.
-
compound-types/slice
Learning Rust By Practice, narrowing the gap between beginner and skilled-dev with challenging examples, exercises and projects.
https://zh.practice.rs/compound-types/slice.html
Beta Was this translation helpful? Give feedback.
All reactions