Skip to content

Commit 8d52a62

Browse files
committed
slice one
1 parent 103e27c commit 8d52a62

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+43
-5
lines changed

.vscode/settings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{
2+
}

rust-by-example/common/src/main.rs

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,21 +109,57 @@ fn main() {
109109

110110
let s = String::from("hello");
111111

112-
change(&s);
113-
114112
let mut s = String::from("hello");
115113

116114
change_mut(&mut s);
117115

116+
let mut s = String::from("hello");
117+
118+
{
119+
let r1 = &mut s; // r1 在这里离开了作用域,所以我们完全可以创建一个新的引用
120+
121+
}
122+
123+
let r2 = &mut s;
124+
125+
let mut ss = String::from("hello");
126+
127+
// let r1 = &ss; // no problem 不可变引用
128+
// let r2 = &ss; // no problem 不可变引用
129+
// let r3 = &mut ss; // BIG PROBLEM 可变引用
130+
131+
// println!("{}, {}, and {}", r1, r2, r3);
132+
133+
let mut sb = String::from("hello world");
134+
135+
let word = first_word(&s);
136+
137+
s.clear(); // 清空了字符串,使其等于 ""
138+
139+
print!("The word value: {}", word);
140+
141+
142+
}
143+
144+
fn first_word(s: &String) -> usize {
145+
let bytes = s.as_bytes();
146+
147+
for(i, &item) in bytes.iter().enumerate() {
148+
if item == b' '{
149+
return i;
150+
}
151+
}
152+
153+
s.len()
118154
}
119155

120156
fn change_mut(s: &mut String) {
121157
s.push_str(", world");
122158
}
123159

124-
fn change(some_string: &String) {
125-
some_string.push_str(", world"); // 报错 不允许修改引用的值
126-
}
160+
// fn change(some_string: &String) {
161+
// some_string.push_str(", world"); // 报错 不允许修改引用的值
162+
// }
127163

128164
fn calculate_length(s: String) -> (String, usize) {
129165
let length = s.len();
27.4 KB
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)