File tree Expand file tree Collapse file tree 1 file changed +11
-13
lines changed
Expand file tree Collapse file tree 1 file changed +11
-13
lines changed Original file line number Diff line number Diff line change 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-
131trait 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) ]
2018mod tests {
You can’t perform that action at this time.
0 commit comments