File tree Expand file tree Collapse file tree 1 file changed +6
-13
lines changed
Expand file tree Collapse file tree 1 file changed +6
-13
lines changed Original file line number Diff line number Diff line change 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-
121trait AppendBar {
132 fn append_bar ( self ) -> Self ;
143}
154
165impl 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
2013fn 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) ]
You can’t perform that action at this time.
0 commit comments