mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-25 23:10:30 +00:00
traits2 solution
This commit is contained in:
parent
92f249a52c
commit
091e1e7f7a
3 changed files with 37 additions and 19 deletions
|
@ -1,14 +1,9 @@
|
||||||
// Your task is to implement the trait `AppendBar` for a vector of strings. To
|
|
||||||
// implement this trait, consider for a moment what it means to 'append "Bar"'
|
|
||||||
// to a vector of strings.
|
|
||||||
//
|
|
||||||
// No boiler plate code this time, you can do this!
|
|
||||||
|
|
||||||
trait AppendBar {
|
trait AppendBar {
|
||||||
fn append_bar(self) -> Self;
|
fn append_bar(self) -> Self;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Implement trait `AppendBar` for a vector of strings.
|
// TODO: Implement the trait `AppendBar` for a vector of strings.
|
||||||
|
// `appned_bar` should push the string "Bar" into the vector.
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// You can optionally experiment here.
|
// You can optionally experiment here.
|
||||||
|
@ -21,7 +16,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn is_vec_pop_eq_bar() {
|
fn is_vec_pop_eq_bar() {
|
||||||
let mut foo = vec![String::from("Foo")].append_bar();
|
let mut foo = vec![String::from("Foo")].append_bar();
|
||||||
assert_eq!(foo.pop().unwrap(), String::from("Bar"));
|
assert_eq!(foo.pop().unwrap(), "Bar");
|
||||||
assert_eq!(foo.pop().unwrap(), String::from("Foo"));
|
assert_eq!(foo.pop().unwrap(), "Foo");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -755,21 +755,18 @@ https://doc.rust-lang.org/stable/book/ch10-01-syntax.html#in-method-definitions"
|
||||||
name = "traits1"
|
name = "traits1"
|
||||||
dir = "15_traits"
|
dir = "15_traits"
|
||||||
hint = """
|
hint = """
|
||||||
A discussion about Traits in Rust can be found at:
|
More about traits in The Book:
|
||||||
https://doc.rust-lang.org/book/ch10-02-traits.html
|
https://doc.rust-lang.org/book/ch10-02-traits.html"""
|
||||||
"""
|
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "traits2"
|
name = "traits2"
|
||||||
dir = "15_traits"
|
dir = "15_traits"
|
||||||
hint = """
|
hint = """
|
||||||
Notice how the trait takes ownership of `self`, and returns `Self`.
|
Notice how the trait takes ownership of `self` and returns `Self`.
|
||||||
|
|
||||||
Try mutating the incoming string vector. Have a look at the tests to see
|
Although the signature of `append_bar` in the trait takes `self` as argument,
|
||||||
what the result should look like!
|
the implementation can take `mut self` instead. This is possible because the
|
||||||
|
the value is owned anyway."""
|
||||||
Vectors provide suitable methods for adding an element at the end. See
|
|
||||||
the documentation at: https://doc.rust-lang.org/std/vec/struct.Vec.html"""
|
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "traits3"
|
name = "traits3"
|
||||||
|
|
|
@ -1 +1,27 @@
|
||||||
// Solutions will be available before the stable release. Thank you for testing the beta version 🥰
|
trait AppendBar {
|
||||||
|
fn append_bar(self) -> Self;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AppendBar for Vec<String> {
|
||||||
|
fn append_bar(mut self) -> Self {
|
||||||
|
// ^^^ this is important
|
||||||
|
self.push(String::from("Bar"));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn is_vec_pop_eq_bar() {
|
||||||
|
let mut foo = vec![String::from("Foo")].append_bar();
|
||||||
|
assert_eq!(foo.pop().unwrap(), "Bar");
|
||||||
|
assert_eq!(foo.pop().unwrap(), "Foo");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue