Oldřich Vetešník
2014-09-12 11:35:35 UTC
Hello,
this is probably going to sound silly, but can this be written better?
I?d like to turn ?hello-there? into ?Hello There?.
I was thinking about something along the lines of split -> map -> capitalize -> join but the capitalization seems kind of awful to me.
use std::char;
fn name_from_file(file: String) -> String {
let mut words: Vec<&str> = file.as_slice().split('-').collect();
let mut cap_words = Vec::new();
for word in words.mut_iter() {
let first = char::to_uppercase(word.char_at(0)).to_string();
let rest = word.slice_chars(1, word.len());
let cap_word = first.append(rest);
cap_words.push(cap_word);
}
cap_words.connect(" ")
}
fn main() {
let hypenated_string = "hello-there".to_string();
let capitalized_string = name_from_file(hypenated_string);
println!("{}", capitalized_string);
}
Thanks,
Ollie
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140912/05ba8c5b/attachment.html>
this is probably going to sound silly, but can this be written better?
I?d like to turn ?hello-there? into ?Hello There?.
I was thinking about something along the lines of split -> map -> capitalize -> join but the capitalization seems kind of awful to me.
use std::char;
fn name_from_file(file: String) -> String {
let mut words: Vec<&str> = file.as_slice().split('-').collect();
let mut cap_words = Vec::new();
for word in words.mut_iter() {
let first = char::to_uppercase(word.char_at(0)).to_string();
let rest = word.slice_chars(1, word.len());
let cap_word = first.append(rest);
cap_words.push(cap_word);
}
cap_words.connect(" ")
}
fn main() {
let hypenated_string = "hello-there".to_string();
let capitalized_string = name_from_file(hypenated_string);
println!("{}", capitalized_string);
}
Thanks,
Ollie
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140912/05ba8c5b/attachment.html>