1
Fork 0
leetcode/source/length_of_last_word/mod.rs

18 lines
435 B
Rust
Raw Normal View History

2022-04-07 17:33:45 +00:00
pub fn length_of_last_word(string: String) -> i32 {
string
.split_whitespace()
.last()
.map(|word| word.len())
.unwrap_or_default() as i32
}
#[test]
fn test_length_of_last_word() {
assert_eq!(length_of_last_word("Hello World".to_string()), 5);
assert_eq!(
length_of_last_word(" fly me to the moon ".to_string()),
4
);
assert_eq!(length_of_last_word("luffy is still joyboy".to_string()), 6);
}