Created
January 22, 2016 21:37
-
-
Save anonymous/09524958b7600ac00363 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Substring { | |
fn substr(&self, start_index: u32, length: u32) -> &str; | |
fn substring(&self, start_index: u32) -> &str; | |
} | |
impl Substring for str { | |
fn substr(&self, start_index: u32, length: u32) -> &str { | |
if length == 0 { return ""; } | |
if start_index == 0 && length == self.len() as u32 { return &self; } | |
return &self[start_index as usize .. start_index as usize + length as usize]; | |
} | |
fn substring(&self, start_index: u32) -> &str { | |
return &self.substr(start_index, self.len() as u32 - start_index); | |
} | |
} | |
fn main() { | |
let string = "The quick brown fox jumped over the lazy dog"; | |
println!("Substring: {}", string.substring(5)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment