On this page
article
Functions and Strings
Functions and Strings
Passing Primitive String - String Literal (&str)
String literals are passed to the functions just like other variables. They can be reused after the function call.
fn main(){
let course: &str = "Rust Programming";
display_course_name(course);
println!("{}",course); // string literal is used after the function call
}
fn display_course_name(my_course: &str){
println!("Course : {}", my_course);
}
output
Course : Rust Programming
Rust Programming
Passing Growable String - String Object (String)
While passing String Objects to functions, they cannot be reused again because the value once passed gets moved to that function’s scope and cannot be reused.
fn main(){
let course:String = String::from("Rust Programming");
display_course_name(course);
//cannot access course after display
}
fn display_course_name(my_course:String){
println!("Course : {}", my_course);
}
output
Course : Rust Programming
Last updated 25 Jan 2024, 05:11 +0530 .