Literals/String: Difference between revisions

Added Rust example
(Add Plain English)
(Added Rust example)
Line 2,546:
This will not interpolate: #{a}
NON_INTERPOLATING</syntaxhighlight>
 
=={{header|Rust}}==
 
A <code>char</code> in Rust is a Unicode scalar value. A char type in Rust is always four bytes in size and can be denoted by single quotes:
 
<syntaxhighlight lang="rust">
let char01: char = 'a';
let char02: char = '\u{25A0}'; // Black square
let char03: char = '❤'; // Heart
</syntaxhighlight>
 
Rust has two common string types: <code>&str</code> and <code>String</code>. These different string types are used depending if it's a fixed string literal that is saved into the executable and lives throughout the program execution (usually borrowed as a <code>&str</code>), a string slice that references a contiguous sequence of elements (borrowed as a <code>&str</code>), or a <code>String</code> which allows for a growable heap allocated valid UTF-8 string. Only the <code>String</code> type is fully owned by its variable, and the other two are only interacted through the reference <code>&str</code>. The <code>&str</code> string slice type may point to a string literal or a heap-allocated string.
 
The String type in Rust is intended to always contain a valid UTF-8 string. UTF-8 is a "variable width" encoding, and therefore Strings are going to be typically smaller than an array of the same Rust <code>char</code>'s. For example, the String "hi" is all within ASCII, and therefore a UTF-8 String of "hi" represents each character as one byte each. On the other hand, a char array ['h', 'i'] would take up 8 bytes in total. A string is denoted by double quotes:
 
<syntaxhighlight lang="rust">
const string_literal_str1: &str = "Hi Rust!";
 
let string_slice_str1: &str = string_literal_str1; // Creating a string slice from a string literal
let string_slice_str2: &str = "hello str"; // String slice pointing to string literal "hello str"
 
let string1: String = "hi".to_string();
let string2: String = "bye".to_owned();
let string3: 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 string4: 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 = &string1; // Creating a string slice to a heap-allocated String.
let string5: 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>
 
Rust supports verbatim strings and here-strings by putting <code>r#</code> before the first double quotes, and the end is marked by adding a <code>#</code> after the final double quotes. If there is a <code>"#</code> inside the contents of your here-string or your verbatim string then you can just add more <code>#</code>'s as required in both the beggining and the end:
<syntaxhighlight lang="rust">
let verbatim_here_string01: &str = r#"A \verbatim string\, line breaks in programming use \n and tabs \t"#;
let verbatim_here_string02: &str = r#"
A \multi-line\ string, in programming
line breaks use the characters \n
and for tabs we use the characters \t
"#;
let verbatim_here_string03: &str = r##"
Part number "#001": 1
Part number "#002": 2
Part number "#003": 3
"##;
</syntaxhighlight>
 
To expand variables in Rust we have 3 options: we can use the "format!" macro, the "print!" macro or the "println!" macro.
<syntaxhighlight lang="rust">
let number: i32 = 42;
let number_string: String = format!("Number: {}", number);
println!("The result in string form is '{}'.", number_string);
print!("The number is {}. ", number); // Print without line break
println!("Again, it's {}", number); // Print with line break
// The above prints:
// The result in string form is 'Number: 42'.
// The number is 42. Again, it's 42
</syntaxhighlight>
 
In Rust, there are other string types that are specialized to specific string requirements, such as working with system strings (OsString and OsStr), working with C strings (CString and CStr), and working with system paths (Path and PathBuf).
 
=={{header|S-lang}}==