Literals/String: Difference between revisions
→{{header|Rust}}
(Added Rust example) |
|||
Line 2,567:
let string_slice_str2: &str = "hello str"; // String slice pointing to string literal "hello str"
let string1: String =
let string2: String = String::from("
let string3: 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
// String slices can also point to heap allocated strings:
let string_slice_str3: &str = &
let
</syntaxhighlight>
|