100-exercises-to-learn-rust/exercises/04_traits/07_deref/src/lib.rs

39 lines
1011 B
Rust
Raw Normal View History

2024-05-13 04:21:03 +08:00
// TODO: whenever `title` and `description` are returned via their accessor methods, they
// should be normalized—i.e. leading and trailing whitespace should be removed.
// There is a method in Rust's standard library that can help with this, but you won't
// find it in the documentation for `String`.
// Can you figure out where it is defined and how to use it?
pub struct Ticket {
title: String,
description: String,
status: String,
}
impl Ticket {
pub fn title(&self) -> &str {
2024-10-22 20:37:18 +08:00
self.title.trim()
2024-05-13 04:21:03 +08:00
}
pub fn description(&self) -> &str {
2024-10-22 20:37:18 +08:00
self.description.trim()
2024-05-13 04:21:03 +08:00
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_normalization() {
let ticket = Ticket {
title: " A title ".to_string(),
description: " A description ".to_string(),
status: "To-Do".to_string(),
};
assert_eq!("A title", ticket.title());
assert_eq!("A description", ticket.description());
}
}