Created
March 9, 2018 11:24
-
-
Save shivshankar3578/5b1c8b914999264450700f740526eeab to your computer and use it in GitHub Desktop.
rust
This file contains 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
// use std::fmt; | |
fn main() { | |
let name = format!("shiv shankar"); | |
let new_name :String ; | |
println!("actual name = {:?}",name ); | |
// shadow of name with mut | |
let (mut name, title_name) = title_case(name); | |
{ | |
// passing &mut data tmp block access of varible | |
// remove new scope and see magic | |
let r = &mut name; | |
new_name = change_name(r); | |
// let r2 = &mut name; // panic! only one &mut ref allow at once | |
} | |
println!("new socpe name = {:?}",name ); | |
println!("title name = {:?}",title_name ); | |
println!("new name = {:?}",new_name ); | |
} | |
fn change_name(name:&mut String) -> String { | |
let mut new_name = name.clone(); | |
// new_name =new_name[1..].to_string().push('!'); | |
// println!("{:?} ",new_name ); | |
// change actual name | |
name.push_str(" sharma"); | |
// return new_name | |
new_name | |
} | |
fn title_case(name:String) -> (String, String) { | |
let mut title_name = String::new(); | |
for word in name.split(" ") { | |
title_name.push_str(&word.to_uppercase()); | |
// notice " " is string while ' ' is space char | |
title_name.push(' '); | |
} | |
(name, title_name) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment