Skip to content

Commit 07234cf

Browse files
authored
Update traits1.rs
1 parent 4ccefe0 commit 07234cf

File tree

1 file changed

+6
-13
lines changed

1 file changed

+6
-13
lines changed

exercises/traits/traits1.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
1-
// traits1.rs
2-
//
3-
// Time to implement some traits! Your task is to implement the trait
4-
// `AppendBar` for the type `String`. The trait AppendBar has only one function,
5-
// which appends "Bar" to any object implementing this trait.
6-
//
7-
// Execute `rustlings hint traits1` or use the `hint` watch subcommand for a
8-
// hint.
9-
10-
// I AM NOT DONE
11-
121
trait AppendBar {
132
fn append_bar(self) -> Self;
143
}
154

165
impl AppendBar for String {
17-
// TODO: Implement `AppendBar` for type `String`.
6+
// 实现append_bar方法:在字符串末尾追加"Bar"并返回新字符串
7+
fn append_bar(self) -> Self {
8+
// self是String类型,拼接"Bar"后返回新的String
9+
self + "Bar"
10+
}
1811
}
1912

2013
fn main() {
2114
let s = String::from("Foo");
2215
let s = s.append_bar();
23-
println!("s: {}", s);
16+
println!("s: {}", s); // 输出:s: FooBar
2417
}
2518

2619
#[cfg(test)]

0 commit comments

Comments
 (0)