Skip to content

Instantly share code, notes, and snippets.

@brson
Created August 24, 2011 01:04
Show Gist options
  • Save brson/1167061 to your computer and use it in GitHub Desktop.
Save brson/1167061 to your computer and use it in GitHub Desktop.
typestate in std::str::replace
fn replace(s: &istr, from: &istr, to: &istr) : is_not_empty(from) -> istr {
if byte_len(s) == 0u {
ret ~"";
} else if starts_with(s, from) {
check (is_not_empty(from));
ret to + replace(slice(s, byte_len(from), byte_len(s)), from, to);
} else {
check (is_not_empty(from));
ret unsafe_from_byte(s[0]) +
replace(slice(s, 1u, byte_len(s)), from, to);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment