Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'7.3. String、&str 和 str' 内容有误导 #1245

Open
idlercloud opened this issue Jul 6, 2023 · 1 comment
Open

'7.3. String、&str 和 str' 内容有误导 #1245

idlercloud opened this issue Jul 6, 2023 · 1 comment

Comments

@idlercloud
Copy link
Contributor

str 类型是硬编码进可执行文件,也无法被修改

这一句是比较显然的错误。作者希望表述的应该是字符串字面量,如 "abc" 这样直接包含在源代码中的字符串,其类型为 &'static str。(注意,这并不意味着有 'static 生命周期的 str 就不可变了,仍然有办法构造出具有 'static 生命周期的 &mut str

然而 str 类型本身是可以修改的,可以试着用 Box<str> 调用 make_ascii_uppercase() 验证。

正如 String 只是 Vec<u8> 的包装,str 也基本上只是 [u8] 的一个子集,[u8] 可变,str 自然也可变,只是依旧要遵循 utf8 编码罢了。

@sunface
Copy link
Owner

sunface commented Jul 17, 2023

谢谢提醒,我看一下哈。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants