Skip to content

Instantly share code, notes, and snippets.

@chikoski
Created December 10, 2015 04:36
Show Gist options
  • Save chikoski/9d29be8f0f61b0b567e3 to your computer and use it in GitHub Desktop.
Save chikoski/9d29be8f0f61b0b567e3 to your computer and use it in GitHub Desktop.
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