basic/compound-type/string-slice #676
Replies: 65 comments 84 replies
-
可以补充一下字符串转义方面的内容吗? |
Beta Was this translation helpful? Give feedback.
-
fn main() { fn first_word(s: &String) -> &str { 关于这一段不是很理解,按照上一讲的说法,&s只在 //2 出现,之后就没有再使用了,范围应该是只在//2,&mut s的范围在 //3,这里报错是因为rust知道word是s的不可变引用,所以&s的范围是从 //2到 //4吗?rust是怎么知道的 |
Beta Was this translation helpful? Give feedback.
-
”+ 和 += 都是返回一个新的字符串。所以变量声明可以不需要 mut 关键字修饰。“,但是代码里就是用了mut,而且result还变了?这写错了? |
Beta Was this translation helpful? Give feedback.
-
少了一个“节“: |
Beta Was this translation helpful? Give feedback.
-
"数组切片和字符串切片的工作方式是一样的",有一些具体的细节还是没讲清楚: |
Beta Was this translation helpful? Give feedback.
-
关于字符串连接的第一个代码示例中的注释——” &string_rust会自动解引用为&str“。 |
Beta Was this translation helpful? Give feedback.
-
remove()函数返回的不是字符串,是字符类型。 |
Beta Was this translation helpful? Give feedback.
-
为什么String可变,声明的时候还要加mut呢 |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
为啥内部同时要弄utf8和unicode两种编码。。 |
Beta Was this translation helpful? Give feedback.
-
这里s1的所有权是被转移到了add方法的self? 理解起来有点怪。 |
Beta Was this translation helpful? Give feedback.
-
我又一个不成熟的问题、 |
Beta Was this translation helpful? Give feedback.
-
既然不支持索引,为什么要支持切片呢(切片的错误发生在run time,索引直接就在compile time了,也放到run time不好么,或者统一放到compile time) |
Beta Was this translation helpful? Give feedback.
-
有个问题想请教一下, 如果定义一个字符串引用, 例如 |
Beta Was this translation helpful? Give feedback.
-
既然String是可变的,那对String的不可变引用&str是不是把原来存在堆上的数据重新copy了一份到栈上? |
Beta Was this translation helpful? Give feedback.
-
fn main() {
let mut s = String::from("hello world");
let word = first_word(&s);
s.clear(); // error!
println!("the first word is: {}", word);
}
fn first_word(s: &String) -> &str {
&s[..1]
} 这段代码看似简单实则不简单,这里面蕴含了两个值得思考的地方:
|
Beta Was this translation helpful? Give feedback.
-
插入方法这里不严谨:根据源码定义,insert方法的index是指字节位置,不是字符位置。字符表示的是’中‘, ’国‘,’人‘ (🇨🇳emoji占用两个字符,打不出来😂)
示例代码:
|
Beta Was this translation helpful? Give feedback.
-
“可以考虑尝试下这个库:utf8_slice。” |
Beta Was this translation helpful? Give feedback.
-
作为一个iOS开发,跟swift对比着看文章。虽然很多相似的概念,但就字符串这一节看下来,swift真的是太友好了,容易入门多了。 |
Beta Was this translation helpful? Give feedback.
-
这设计的怎么那么矛盾呢,字符串不能索引,但是操作字符串的方法又需要传递索引参数,设计者估计都不知道怎么会这样吧 |
Beta Was this translation helpful? Give feedback.
-
Mark: 如果问你该字符串多长,你可能会说 3,但是实际上是 9 个字节的长度,因为大部分常用汉字在 UTF-8 中的长度是 3 个字节,因此这种情况下对 hello 进行索引,访问 &hello[0] 没有任何意义,因为你取不到 中 这个字符,而是取到了这个字符三个字节中的第一个字节,这是一个非常奇怪而且难以理解的返回值。 |
Beta Was this translation helpful? Give feedback.
-
最后一个String的联系链接404了 |
Beta Was this translation helpful? Give feedback.
-
很奇怪啊,rust中pop的实现:```rust
|
Beta Was this translation helpful? Give feedback.
-
最后一个练习的链接似乎需要从大写S改成小写s。 |
Beta Was this translation helpful? Give feedback.
-
切片后的字符串是什么类型呢? 内存是怎么管理的, 是跟原字符串共用的一片内存吗? 因为我原先是学习C的, C里面\0表示字符串结束, 那么在这里如果共用的话, rust是字符串是有自己的逻辑来判断字符串的终止? |
Beta Was this translation helpful? Give feedback.
-
String的课后作业页面Page Not Found 了 |
Beta Was this translation helpful? Give feedback.
-
rust的字符串比js、java复杂太多了。 |
Beta Was this translation helpful? Give feedback.
-
insert的第一个参数是字节位置,如果字符串中是中文,会崩溃!!! insert说明 |
Beta Was this translation helpful? Give feedback.
-
fn string_add() -> () {
let test_add = String::from("qqq123");
let str_rht = String::from("rust");
let test_add = test_add + str_rht.as_str();
println!("test_add: {}", test_add);
} 请容许我借这段代码向您讲解一下,rust的mut机制。 fn string_add() -> () {
let test_add = String::from("qqq123");
let str_rht = String::from("rust");
let test_add = test_add + str_rht.as_str();
str_rht.push_str("dsad");
println!("test_add: {}", test_add);
} 我们新增了一个str_rht.push_str, 请你判断这个函数是否能够被成功编译? 那么根据我上面所讲述的,请你改写这一句使可以通过编译吧。 |
Beta Was this translation helpful? Give feedback.
-
刚学到这里,字符串删除方法中说 |
Beta Was this translation helpful? Give feedback.
-
basic/compound-type/string-slice
https://course.rs/basic/compound-type/string-slice.html
Beta Was this translation helpful? Give feedback.
All reactions