Files
rustlings/exercises/15_traits/traits2.rs
T

29 lines
615 B
Rust
Raw Normal View History

2026-03-12 20:54:14 -04:00
trait AppendBar {
fn append_bar(self) -> Self;
}
// TODO: Implement the trait `AppendBar` for a vector of strings.
// `append_bar` should push the string "Bar" into the vector.
2026-03-23 03:36:33 -04:00
impl AppendBar for Vec<String> {
fn append_bar(mut self) -> Self {
self.push("Bar".into());
self
}
}
2026-03-12 20:54:14 -04:00
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");
}
}