Created
December 10, 2015 04:36
-
-
Save chikoski/9d29be8f0f61b0b567e3 to your computer and use it in GitHub Desktop.
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
pub fn main() { | |
let mut str1 = format!("fellow "); | |
//let str2 = format!("Rustaceans"); | |
let str2 = str1.clone(); | |
strcat(&mut str1, &str2); | |
println!("{}", str1); | |
println!("{}", concat(&str1, &str1)); | |
} | |
/// Concatenate `suffix` onto the end of `prefix`. | |
fn strcat(prefix: &mut String, suffix: &String){ | |
for ch in suffix.chars() { | |
prefix.push(ch); | |
} | |
} | |
fn concat(car: &String, cdr: &String) -> String{ | |
let mut result = format!(""); | |
for ch in car.chars(){ | |
result.push(ch); | |
} | |
for ch in cdr.chars(){ | |
result.push(ch); | |
} | |
result | |
} | |
// Challenge: Convert `strcat` to use borrowing, not ownership. | |
// Question: Now that you've converted `strcat`, what happens if you | |
// call `strcat` using the same string for `prefix` and `suffix`? | |
// Why? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment