From 1e1a65eaf06d5691001d1ec62aa4fb3d61b0d41c Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Tue, 25 Dec 2018 23:57:11 +0100 Subject: capitalize() would fail if first char were not ascii. Enhanced version using chain iterator. --- src/main.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index a76b378..c33924d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,12 +25,21 @@ use std::string::ToString; mod locale; fn capitalize(s: &str) -> String { - match s.chars().next() { - Some(ch) => ch.to_uppercase().collect::() + &s[1..], + let mut chars = s.chars(); + match chars.next() { + Some(ch) => ch.to_uppercase().chain(chars).collect::(), None => String::new(), } } +#[test] +fn test_capitalize() { + assert_eq!("Flaccid", capitalize("flaccid")); + assert_eq!("Ångström", capitalize("ångström")); + assert_eq!("Å i lofoten", capitalize("å i lofoten")); + assert_eq!("Ååååh", capitalize("ååååh")); +} + fn print_entry(d: &serde_yaml::Value) { let name = d.get("name").unwrap(); println!("{}", capitalize(name.as_str().unwrap())); -- cgit v1.2.3