Skip to content

Commit 9379679

Browse files
authored
Update traits2.rs
1 parent 07234cf commit 9379679

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

exercises/traits/traits2.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
// traits2.rs
2-
//
3-
// Your task is to implement the trait `AppendBar` for a vector of strings. To
4-
// implement this trait, consider for a moment what it means to 'append "Bar"'
5-
// to a vector of strings.
6-
//
7-
// No boiler plate code this time, you can do this!
8-
//
9-
// Execute `rustlings hint traits2` or use the `hint` watch subcommand for a hint.
10-
11-
// I AM NOT DONE
12-
131
trait AppendBar {
142
fn append_bar(self) -> Self;
153
}
164

17-
// TODO: Implement trait `AppendBar` for a vector of strings.
5+
// 为Vec<String>实现AppendBar trait
6+
impl AppendBar for Vec<String> {
7+
fn append_bar(self) -> Self {
8+
// 复制self到可变变量中(因为self是不可变的)
9+
let mut vec = self;
10+
// 向向量末尾添加字符串"Bar"
11+
vec.push(String::from("Bar"));
12+
// 返回修改后的向量
13+
vec
14+
}
15+
}
1816

1917
#[cfg(test)]
2018
mod tests {

0 commit comments

Comments
 (0)