Literals/String: Difference between revisions

(Added Rust example)
Line 2,567:
let string_slice_str2: &str = "hello str"; // String slice pointing to string literal "hello str"
 
let string1: String = "hi".to_stringString::new(); // Empty String
let string2: String = String::from("byehello".to_owned(); // Creating String from string literal "hello"
let string3: String = "see you soonhi".intoto_string();
let string4: String = "bye".to_owned();
let string5: String = "see you soon".into();
// The "to_string()", "to_owned" or "into" are all equivalent in the code above.
// The "to_string()", "to_owned" or "into" methods are needed so that a string slice (&str) or a string literal (&str) is explicitly converted into a heap-allocated fully-owned String type. Otherwise the compiler's type checker will complain "expected struct `String`, found `&str` (string slice)"
 
let string4string6: String = string_slice_str2.to_owned(); // Explictly converting the string_slice_str2 into a heap-allocated fully-owned String. This can be done with "to_string()", "to_owned" or "into".
 
// String slices can also point to heap allocated strings:
let string_slice_str3: &str = &string1string2; // Creating a string slice to a heap-allocated String.
let string5string7: String = string_slice_str3.to_string(); // Converting string_slice_str3 into a heap-allocated fully-owned String copy, resulting in a new independent owned string copy of the original String. This can be done with "to_string()", "to_owned" or "into".
</syntaxhighlight>