, .
a Option<&String>. &String &str, &str &String. :
fn main() {
let a = String::from("This is a String");
let a_r = &a;
foo(a_r);
bar(a_r);
let b = "This is an &str";
bar(b);
}
fn foo(s: &String) {
println!("Foo: {}", s);
}
fn bar(s: &str) {
println!("Bar: {}", s);
}
foo(b); , , String, str.
match &str ( &String), unwrap_or , Option, &String, &str (String::from, to_string, to_owned ..)
, , , .
("foo") , . - &str, str. , , . , , , - - ("fooquxegg") - , , .
A String, , . , String, "", , - " " - , String , , Box<T> T .
, &String &str ( -, - , ), &str &String. (, "foo"), , , &String, , (, "bar"), - .
:
fn main() {
let s = "this is an &str";
let b = String::from("this is a String");
let s_o : Option<&str> = Some(s);
let s_or_def = s_o.unwrap_or(&b);
println!("{}", s_or_def);
}
unwrap_or: A Option<&str>, &String . , &String => &str , , . , , unwrap_or , ; , T ( T &str).
, unwrap_or , &str &String, , unwrap_or . , , unwrap_or String. to_string , :
fn main() {
let s = String::from("Hello, world!");
let o = Some(s);
let r = o.unwrap_or("Goodbye, world!".to_string());
println!("{}", r);
}
map_or, , |s| &s ( ).