Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::borrow::Cow;
- fn remove_spaces<'a, T: 'a + AsRef<str>>(input: & 'a T) -> Cow<'a, str> {
- if input.as_ref().contains(' ') {
- let mut buf = String::with_capacity(input.as_ref().len());
- for c in input.as_ref().chars() {
- if c!= ' ' {
- buf.push(c);
- }
- }
- return Cow::Owned(buf);
- }
- return Cow::Borrowed(input.as_ref());
- }
- fn main() {
- let x = "he llo,wor ld!".to_string();
- let ret = remove_spaces(&x);
- println!("{:?}", ret);
- }
Add Comment
Please, Sign In to add comment