Pure Programmer
Blue Matrix


Cluster Map

Strings

L1

This page is under construction. Please come back later.

At their heart, a string is simply a sequence of characters. Characters, in turn, are encoded as integers using encoding schemes such as [[ASCII]], [[ISO-Latin-1]] or [[Unicode]]. So a string is simply a sequence of integers. Languages like FORTRAN or C that don't support strings as a fundamental type, actually treat strings as a sequence of integers. Modern programming languages support strings as a fundamental data type which make them much easier to use and manipulate.

Strings are particularly useful when conveying information to the user in contrast to being useful for computation like integers or floating point values. While strings are typically implemented as an array/list of the more basic character type, modern languages provide a string data type that encapsulates the characters of a string and provide functionality for working with strings. When using strings in our programs there are two types of strings: [[literal]] strings and string variables.

Variables and Literals

String literals are string values that are directly written in the program itself. String literals only represent the one string as it is typed into the code. String literals are enclosed in double quotes or single quotes. String literals can be used any place a string is required.


Example String Literals

String variables are like any other variable. This means that string variables must be declared before they can be used and must follow the variable naming rules discussed in the variables section. that is they can be set and reset to different values at will. Unlike variables of the primitive types such as boolean, integer and floating point, string variables represent an entire string of characters. String variables can be set using string literals or other string variables.

Concatenation

One of the most basic operations we can perform on strings is called concatenation. Concatenation takes two strings and forms a new one by appending the second string on to the end of the first string. Using concatenation we can build up a complex string from small strings. In Rust the concatenation operator is the + (plus) symbol.

Strings1.rs
/******************************************************************************
 * This program demonstrates string concatenation.
 * 
 * Copyright © 2020 Richard Lesh.  All rights reserved.
 *****************************************************************************/

fn main() {
	let a:&str = "one";
	let b:&str = "two";
	let c:&str = "three";
	let mut d:String = String::from(a) + b + c;
	println!("{}", d);
	d = String::from(a) + "\t" + b + "\n" + c;
	println!("{}", d);
}

Output
$ rustc Strings1.rs $ ./Strings1 onetwothree one two three

Operations on Strings

Rust supports a number of operations on strings. The most often used is determining the length of a string. [an error occurred while processing this directive] Counting the characters in a string is accomplished using the length() method. Some of the other string operations available are summarized in the following table.

String Operations
Fixed PointScientific Notation
00e0
3.14153.1415e0
Strings2.rs
/******************************************************************************
 * This program demonstrates basic string functions.
 * 
 * Copyright © 2020 Richard Lesh.  All rights reserved.
 *****************************************************************************/

fn main() {
	let alphabet:&str = "abcdefghijklmnopqrstuvwxyzabc";
	let greekAlphabet:&str = "αβγδεζηθικλμνξοπρσςτυφχψωαβγ";
	let emoji:&str = "😃😇🥰🤪🤑😴🤒🥵🥶🤯🥳😎😥😱😡🤬💀👽🤖😺🙈🙉🙊😃😇🥰";

	println!("Length: {}", strlen(alphabet));
	println!("charAt(17): {}", alphabet[17]);
	println!("codePointAt(17): {}", alphabet[17]);
	println!("substr(23, 26): {}", substr(alphabet,23,26));
	println!("prefix(6): {}", prefix(alphabet,6));
	println!("right_tail(6): {}", right_tail(alphabet,6));
	println!("suffix(6): {}", suffix(alphabet,6));
	println!("find(\'def\'): {}", find(alphabet,"def"));
	println!("find(\'def\') is not found: {}", (find(alphabet,"def") == string::npos));
	println!("find(\'bug\'): {}", find(alphabet,"bug"));
	println!("find(\'bug\') is not found: {}", (find(alphabet,"bug") == string::npos));
	println!("rfind(\'abc\'): {}", rfind(alphabet,"abc"));
	println!("rfind(\'abc\') is not found: {}", (rfind(alphabet,"abc") == string::npos));
	println!("rfind(\'bug\'): {}", rfind(alphabet,"bug"));
	println!("rfind(\'bug\') is not found: {}", (rfind(alphabet,"bug") == string::npos));

	println!("Length: {}", strlen(greekAlphabet));
	println!("charAt(17): {}", greekAlphabet[17]);
	println!("codePointAt(17): {}", greekAlphabet[17]);
	println!("substr(23, 26): {}", substr(greekAlphabet,23,26));
	println!("prefix(6): {}", prefix(greekAlphabet,6));
	println!("right_tail(6): {}", right_tail(greekAlphabet,6));
	println!("suffix(6): {}", suffix(greekAlphabet,6));
	println!("find(\'δεζ\'): {}", find(greekAlphabet,"δεζ"));
	println!("find(\'δεζ\') is not found: {}", (find(greekAlphabet,"δεζ") == string::npos));
	println!("find(\'bug\'): {}", find(greekAlphabet,"bug"));
	println!("find(\'bug\') is not found: {}", (find(greekAlphabet,"bug") == string::npos));
	println!("rfind(\'αβγ\'): {}", rfind(greekAlphabet,"αβγ"));
	println!("rfind(\'αβγ\') is not found: {}", (rfind(greekAlphabet,"αβγ") == string::npos));
	println!("rfind(\'bug\'): {}", rfind(greekAlphabet,"bug"));
	println!("rfind(\'bug\') is not found: {}", (rfind(greekAlphabet,"bug") == string::npos));

	println!("Length: {}", strlen(emoji));
	println!("charAt(16): {}", emoji[16]);
	println!("codePointAt(16): {}", emoji[16]);
	println!("substr(20, 24): {}", substr(emoji,20,24));
	println!("prefix(6): {}", prefix(emoji,6));
	println!("right_tail(6): {}", right_tail(emoji,6));
	println!("suffix(6): {}", suffix(emoji,6));
	println!("find(\'😱😡🤬\'): {}", find(emoji,"😱😡🤬"));
	println!("find(\'😱😡🤬\') is not found: {}", (find(emoji,"😱😡🤬") == string::npos));
	println!("find(\'bug\'): {}", find(emoji,"bug"));
	println!("find(\'bug\') is not found: {}", (find(emoji,"bug") == string::npos));
	println!("rfind(\'😃😇🥰\'): {}", rfind(emoji,"😃😇🥰"));
	println!("rfind(\'😃😇🥰\') is not found: {}", (rfind(emoji,"😃😇🥰") == string::npos));
	println!("rfind(\'bug\'): {}", rfind(emoji,"bug"));
	println!("rfind(\'bug\') is not found: {}", (rfind(emoji,"bug") == string::npos));
}

Output
$ rustc Strings2.rs error[E0425]: cannot find function `strlen` in this scope --> Strings2.rs:12:25 | 12 | println!("Length: {}", strlen(alphabet)); | ^^^^^^ not found in this scope error[E0277]: the type `str` cannot be indexed by `{integer}` --> Strings2.rs:13:38 | 13 | println!("charAt(17): {}", alphabet[17]); | ^^ string indices are ranges of `usize` | = help: the trait `SliceIndex<str>` is not implemented for `{integer}` = note: you can use `.chars().nth()` or `.bytes().nth()` for more information, see chapter 8 in The Book: <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings> = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `str` to implement `Index<{integer}>` error[E0277]: the type `str` cannot be indexed by `{integer}` --> Strings2.rs:14:43 | 14 | println!("codePointAt(17): {}", alphabet[17]); | ^^ string indices are ranges of `usize` | = help: the trait `SliceIndex<str>` is not implemented for `{integer}` = note: you can use `.chars().nth()` or `.bytes().nth()` for more information, see chapter 8 in The Book: <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings> = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `str` to implement `Index<{integer}>` error[E0425]: cannot find function `substr` in this scope --> Strings2.rs:15:33 | 15 | println!("substr(23, 26): {}", substr(alphabet,23,26)); | ^^^^^^ not found in this scope error[E0425]: cannot find function `prefix` in this scope --> Strings2.rs:16:28 | 16 | println!("prefix(6): {}", prefix(alphabet,6)); | ^^^^^^ not found in this scope error[E0425]: cannot find function `right_tail` in this scope --> Strings2.rs:17:32 | 17 | println!("right_tail(6): {}", right_tail(alphabet,6)); | ^^^^^^^^^^ not found in this scope error[E0425]: cannot find function `suffix` in this scope --> Strings2.rs:18:28 | 18 | println!("suffix(6): {}", suffix(alphabet,6)); | ^^^^^^ not found in this scope error[E0425]: cannot find function `find` in this scope --> Strings2.rs:19:32 | 19 | println!("find(\'def\'): {}", find(alphabet,"def")); | ^^^^ not found in this scope | help: use the `.` operator to call the method `find` on `&str` | 19 - println!("find(\'def\'): {}", find(alphabet,"def")); 19 + println!("find(\'def\'): {}", alphabet.find("def")); | error[E0277]: `Option<usize>` doesn't implement `std::fmt::Display` --> Strings2.rs:19:32 | 19 | println!("find(\'def\'): {}", find(alphabet,"def")); | ^^^^^^^^^^^^^^^^^^^^ `Option<usize>` cannot be formatted with the default formatter | = help: the trait `std::fmt::Display` is not implemented for `Option<usize>` = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0425]: cannot find function `find` in this scope --> Strings2.rs:20:46 | 20 | println!("find(\'def\') is not found: {}", (find(alphabet,"def") == string::npos)); | ^^^^ not found in this scope | help: use the `.` operator to call the method `find` on `&str` | 20 - println!("find(\'def\') is not found: {}", (find(alphabet,"def") == string::npos)); 20 + println!("find(\'def\') is not found: {}", (alphabet.find("def") == string::npos)); | error[E0433]: failed to resolve: use of undeclared crate or module `string` --> Strings2.rs:20:70 | 20 | println!("find(\'def\') is not found: {}", (find(alphabet,"def") == string::npos)); | ^^^^^^ | | | use of undeclared crate or module `string` | help: a struct with a similar name exists (notice the capitalization): `String` error[E0425]: cannot find function `find` in this scope --> Strings2.rs:21:32 | 21 | println!("find(\'bug\'): {}", find(alphabet,"bug")); | ^^^^ not found in this scope | help: use the `.` operator to call the method `find` on `&str` | 21 - println!("find(\'bug\'): {}", find(alphabet,"bug")); 21 + println!("find(\'bug\'): {}", alphabet.find("bug")); | error[E0277]: `Option<usize>` doesn't implement `std::fmt::Display` --> Strings2.rs:21:32 | 21 | println!("find(\'bug\'): {}", find(alphabet,"bug")); | ^^^^^^^^^^^^^^^^^^^^ `Option<usize>` cannot be formatted with the default formatter | = help: the trait `std::fmt::Display` is not implemented for `Option<usize>` = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0425]: cannot find function `find` in this scope --> Strings2.rs:22:46 | 22 | println!("find(\'bug\') is not found: {}", (find(alphabet,"bug") == string::npos)); | ^^^^ not found in this scope | help: use the `.` operator to call the method `find` on `&str` | 22 - println!("find(\'bug\') is not found: {}", (find(alphabet,"bug") == string::npos)); 22 + println!("find(\'bug\') is not found: {}", (alphabet.find("bug") == string::npos)); | error[E0433]: failed to resolve: use of undeclared crate or module `string` --> Strings2.rs:22:70 | 22 | println!("find(\'bug\') is not found: {}", (find(alphabet,"bug") == string::npos)); | ^^^^^^ | | | use of undeclared crate or module `string` | help: a struct with a similar name exists (notice the capitalization): `String` error[E0425]: cannot find function `rfind` in this scope --> Strings2.rs:23:33 | 23 | println!("rfind(\'abc\'): {}", rfind(alphabet,"abc")); | ^^^^^ not found in this scope | help: use the `.` operator to call the method `rfind` on `&str` | 23 - println!("rfind(\'abc\'): {}", rfind(alphabet,"abc")); 23 + println!("rfind(\'abc\'): {}", alphabet.rfind("abc")); | error[E0277]: `Option<usize>` doesn't implement `std::fmt::Display` --> Strings2.rs:23:33 | 23 | println!("rfind(\'abc\'): {}", rfind(alphabet,"abc")); | ^^^^^^^^^^^^^^^^^^^^^ `Option<usize>` cannot be formatted with the default formatter | = help: the trait `std::fmt::Display` is not implemented for `Option<usize>` = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0425]: cannot find function `rfind` in this scope --> Strings2.rs:24:47 | 24 | println!("rfind(\'abc\') is not found: {}", (rfind(alphabet,"abc") == string::npos)); | ^^^^^ not found in this scope | help: use the `.` operator to call the method `rfind` on `&str` | 24 - println!("rfind(\'abc\') is not found: {}", (rfind(alphabet,"abc") == string::npos)); 24 + println!("rfind(\'abc\') is not found: {}", (alphabet.rfind("abc") == string::npos)); | error[E0433]: failed to resolve: use of undeclared crate or module `string` --> Strings2.rs:24:72 | 24 | println!("rfind(\'abc\') is not found: {}", (rfind(alphabet,"abc") == string::npos)); | ^^^^^^ | | | use of undeclared crate or module `string` | help: a struct with a similar name exists (notice the capitalization): `String` error[E0425]: cannot find function `rfind` in this scope --> Strings2.rs:25:33 | 25 | println!("rfind(\'bug\'): {}", rfind(alphabet,"bug")); | ^^^^^ not found in this scope | help: use the `.` operator to call the method `rfind` on `&str` | 25 - println!("rfind(\'bug\'): {}", rfind(alphabet,"bug")); 25 + println!("rfind(\'bug\'): {}", alphabet.rfind("bug")); | error[E0277]: `Option<usize>` doesn't implement `std::fmt::Display` --> Strings2.rs:25:33 | 25 | println!("rfind(\'bug\'): {}", rfind(alphabet,"bug")); | ^^^^^^^^^^^^^^^^^^^^^ `Option<usize>` cannot be formatted with the default formatter | = help: the trait `std::fmt::Display` is not implemented for `Option<usize>` = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0425]: cannot find function `rfind` in this scope --> Strings2.rs:26:47 | 26 | println!("rfind(\'bug\') is not found: {}", (rfind(alphabet,"bug") == string::npos)); | ^^^^^ not found in this scope | help: use the `.` operator to call the method `rfind` on `&str` | 26 - println!("rfind(\'bug\') is not found: {}", (rfind(alphabet,"bug") == string::npos)); 26 + println!("rfind(\'bug\') is not found: {}", (alphabet.rfind("bug") == string::npos)); | error[E0433]: failed to resolve: use of undeclared crate or module `string` --> Strings2.rs:26:72 | 26 | println!("rfind(\'bug\') is not found: {}", (rfind(alphabet,"bug") == string::npos)); | ^^^^^^ | | | use of undeclared crate or module `string` | help: a struct with a similar name exists (notice the capitalization): `String` error[E0425]: cannot find function `strlen` in this scope --> Strings2.rs:28:25 | 28 | println!("Length: {}", strlen(greekAlphabet)); | ^^^^^^ not found in this scope error[E0277]: the type `str` cannot be indexed by `{integer}` --> Strings2.rs:29:43 | 29 | println!("charAt(17): {}", greekAlphabet[17]); | ^^ string indices are ranges of `usize` | = help: the trait `SliceIndex<str>` is not implemented for `{integer}` = note: you can use `.chars().nth()` or `.bytes().nth()` for more information, see chapter 8 in The Book: <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings> = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `str` to implement `Index<{integer}>` error[E0277]: the type `str` cannot be indexed by `{integer}` --> Strings2.rs:30:48 | 30 | println!("codePointAt(17): {}", greekAlphabet[17]); | ^^ string indices are ranges of `usize` | = help: the trait `SliceIndex<str>` is not implemented for `{integer}` = note: you can use `.chars().nth()` or `.bytes().nth()` for more information, see chapter 8 in The Book: <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings> = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `str` to implement `Index<{integer}>` error[E0425]: cannot find function `substr` in this scope --> Strings2.rs:31:33 | 31 | println!("substr(23, 26): {}", substr(greekAlphabet,23,26)); | ^^^^^^ not found in this scope error[E0425]: cannot find function `prefix` in this scope --> Strings2.rs:32:28 | 32 | println!("prefix(6): {}", prefix(greekAlphabet,6)); | ^^^^^^ not found in this scope error[E0425]: cannot find function `right_tail` in this scope --> Strings2.rs:33:32 | 33 | println!("right_tail(6): {}", right_tail(greekAlphabet,6)); | ^^^^^^^^^^ not found in this scope error[E0425]: cannot find function `suffix` in this scope --> Strings2.rs:34:28 | 34 | println!("suffix(6): {}", suffix(greekAlphabet,6)); | ^^^^^^ not found in this scope error[E0425]: cannot find function `find` in this scope --> Strings2.rs:35:32 | 35 | println!("find(\'δεζ\'): {}", find(greekAlphabet,"δεζ")); | ^^^^ not found in this scope | help: use the `.` operator to call the method `find` on `&str` | 35 - println!("find(\'δεζ\'): {}", find(greekAlphabet,"δεζ")); 35 + println!("find(\'δεζ\'): {}", greekAlphabet.find("δεζ")); | error[E0277]: `Option<usize>` doesn't implement `std::fmt::Display` --> Strings2.rs:35:32 | 35 | println!("find(\'δεζ\'): {}", find(greekAlphabet,"δεζ")); | ^^^^^^^^^^^^^^^^^^^^^^^^^ `Option<usize>` cannot be formatted with the default formatter | = help: the trait `std::fmt::Display` is not implemented for `Option<usize>` = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0425]: cannot find function `find` in this scope --> Strings2.rs:36:46 | 36 | println!("find(\'δεζ\') is not found: {}", (find(greekAlphabet,"δεζ") == string::npos)); | ^^^^ not found in this scope | help: use the `.` operator to call the method `find` on `&str` | 36 - println!("find(\'δεζ\') is not found: {}", (find(greekAlphabet,"δεζ") == string::npos)); 36 + println!("find(\'δεζ\') is not found: {}", (greekAlphabet.find("δεζ") == string::npos)); | error[E0433]: failed to resolve: use of undeclared crate or module `string` --> Strings2.rs:36:75 | 36 | println!("find(\'δεζ\') is not found: {}", (find(greekAlphabet,"δεζ") == string::npos)); | ^^^^^^ | | | use of undeclared crate or module `string` | help: a struct with a similar name exists (notice the capitalization): `String` error[E0425]: cannot find function `find` in this scope --> Strings2.rs:37:32 | 37 | println!("find(\'bug\'): {}", find(greekAlphabet,"bug")); | ^^^^ not found in this scope | help: use the `.` operator to call the method `find` on `&str` | 37 - println!("find(\'bug\'): {}", find(greekAlphabet,"bug")); 37 + println!("find(\'bug\'): {}", greekAlphabet.find("bug")); | error[E0277]: `Option<usize>` doesn't implement `std::fmt::Display` --> Strings2.rs:37:32 | 37 | println!("find(\'bug\'): {}", find(greekAlphabet,"bug")); | ^^^^^^^^^^^^^^^^^^^^^^^^^ `Option<usize>` cannot be formatted with the default formatter | = help: the trait `std::fmt::Display` is not implemented for `Option<usize>` = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0425]: cannot find function `find` in this scope --> Strings2.rs:38:46 | 38 | println!("find(\'bug\') is not found: {}", (find(greekAlphabet,"bug") == string::npos)); | ^^^^ not found in this scope | help: use the `.` operator to call the method `find` on `&str` | 38 - println!("find(\'bug\') is not found: {}", (find(greekAlphabet,"bug") == string::npos)); 38 + println!("find(\'bug\') is not found: {}", (greekAlphabet.find("bug") == string::npos)); | error[E0433]: failed to resolve: use of undeclared crate or module `string` --> Strings2.rs:38:75 | 38 | println!("find(\'bug\') is not found: {}", (find(greekAlphabet,"bug") == string::npos)); | ^^^^^^ | | | use of undeclared crate or module `string` | help: a struct with a similar name exists (notice the capitalization): `String` error[E0425]: cannot find function `rfind` in this scope --> Strings2.rs:39:33 | 39 | println!("rfind(\'αβγ\'): {}", rfind(greekAlphabet,"αβγ")); | ^^^^^ not found in this scope | help: use the `.` operator to call the method `rfind` on `&str` | 39 - println!("rfind(\'αβγ\'): {}", rfind(greekAlphabet,"αβγ")); 39 + println!("rfind(\'αβγ\'): {}", greekAlphabet.rfind("αβγ")); | error[E0277]: `Option<usize>` doesn't implement `std::fmt::Display` --> Strings2.rs:39:33 | 39 | println!("rfind(\'αβγ\'): {}", rfind(greekAlphabet,"αβγ")); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ `Option<usize>` cannot be formatted with the default formatter | = help: the trait `std::fmt::Display` is not implemented for `Option<usize>` = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0425]: cannot find function `rfind` in this scope --> Strings2.rs:40:47 | 40 | println!("rfind(\'αβγ\') is not found: {}", (rfind(greekAlphabet,"αβγ") == string::npos)); | ^^^^^ not found in this scope | help: use the `.` operator to call the method `rfind` on `&str` | 40 - println!("rfind(\'αβγ\') is not found: {}", (rfind(greekAlphabet,"αβγ") == string::npos)); 40 + println!("rfind(\'αβγ\') is not found: {}", (greekAlphabet.rfind("αβγ") == string::npos)); | error[E0433]: failed to resolve: use of undeclared crate or module `string` --> Strings2.rs:40:77 | 40 | println!("rfind(\'αβγ\') is not found: {}", (rfind(greekAlphabet,"αβγ") == string::npos)); | ^^^^^^ | | | use of undeclared crate or module `string` | help: a struct with a similar name exists (notice the capitalization): `String` error[E0425]: cannot find function `rfind` in this scope --> Strings2.rs:41:33 | 41 | println!("rfind(\'bug\'): {}", rfind(greekAlphabet,"bug")); | ^^^^^ not found in this scope | help: use the `.` operator to call the method `rfind` on `&str` | 41 - println!("rfind(\'bug\'): {}", rfind(greekAlphabet,"bug")); 41 + println!("rfind(\'bug\'): {}", greekAlphabet.rfind("bug")); | error[E0277]: `Option<usize>` doesn't implement `std::fmt::Display` --> Strings2.rs:41:33 | 41 | println!("rfind(\'bug\'): {}", rfind(greekAlphabet,"bug")); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ `Option<usize>` cannot be formatted with the default formatter | = help: the trait `std::fmt::Display` is not implemented for `Option<usize>` = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0425]: cannot find function `rfind` in this scope --> Strings2.rs:42:47 | 42 | println!("rfind(\'bug\') is not found: {}", (rfind(greekAlphabet,"bug") == string::npos)); | ^^^^^ not found in this scope | help: use the `.` operator to call the method `rfind` on `&str` | 42 - println!("rfind(\'bug\') is not found: {}", (rfind(greekAlphabet,"bug") == string::npos)); 42 + println!("rfind(\'bug\') is not found: {}", (greekAlphabet.rfind("bug") == string::npos)); | error[E0433]: failed to resolve: use of undeclared crate or module `string` --> Strings2.rs:42:77 | 42 | println!("rfind(\'bug\') is not found: {}", (rfind(greekAlphabet,"bug") == string::npos)); | ^^^^^^ | | | use of undeclared crate or module `string` | help: a struct with a similar name exists (notice the capitalization): `String` error[E0425]: cannot find function `strlen` in this scope --> Strings2.rs:44:25 | 44 | println!("Length: {}", strlen(emoji)); | ^^^^^^ not found in this scope error[E0277]: the type `str` cannot be indexed by `{integer}` --> Strings2.rs:45:35 | 45 | println!("charAt(16): {}", emoji[16]); | ^^ string indices are ranges of `usize` | = help: the trait `SliceIndex<str>` is not implemented for `{integer}` = note: you can use `.chars().nth()` or `.bytes().nth()` for more information, see chapter 8 in The Book: <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings> = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `str` to implement `Index<{integer}>` error[E0277]: the type `str` cannot be indexed by `{integer}` --> Strings2.rs:46:40 | 46 | println!("codePointAt(16): {}", emoji[16]); | ^^ string indices are ranges of `usize` | = help: the trait `SliceIndex<str>` is not implemented for `{integer}` = note: you can use `.chars().nth()` or `.bytes().nth()` for more information, see chapter 8 in The Book: <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings> = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `str` to implement `Index<{integer}>` error[E0425]: cannot find function `substr` in this scope --> Strings2.rs:47:33 | 47 | println!("substr(20, 24): {}", substr(emoji,20,24)); | ^^^^^^ not found in this scope error[E0425]: cannot find function `prefix` in this scope --> Strings2.rs:48:28 | 48 | println!("prefix(6): {}", prefix(emoji,6)); | ^^^^^^ not found in this scope error[E0425]: cannot find function `right_tail` in this scope --> Strings2.rs:49:32 | 49 | println!("right_tail(6): {}", right_tail(emoji,6)); | ^^^^^^^^^^ not found in this scope error[E0425]: cannot find function `suffix` in this scope --> Strings2.rs:50:28 | 50 | println!("suffix(6): {}", suffix(emoji,6)); | ^^^^^^ not found in this scope error[E0425]: cannot find function `find` in this scope --> Strings2.rs:51:32 | 51 | println!("find(\'😱😡🤬\'): {}", find(emoji,"😱😡🤬")); | ^^^^ not found in this scope | help: use the `.` operator to call the method `find` on `&str` | 51 - println!("find(\'😱😡🤬\'): {}", find(emoji,"😱😡🤬")); 51 + println!("find(\'😱😡🤬\'): {}", emoji.find("😱😡🤬")); | error[E0277]: `Option<usize>` doesn't implement `std::fmt::Display` --> Strings2.rs:51:32 | 51 | println!("find(\'😱😡🤬\'): {}", find(emoji,"😱😡🤬")); | ^^^^^^^^^^^^^^^^^^^^ `Option<usize>` cannot be formatted with the default formatter | = help: the trait `std::fmt::Display` is not implemented for `Option<usize>` = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0425]: cannot find function `find` in this scope --> Strings2.rs:52:46 | 52 | println!("find(\'😱😡🤬\') is not found: {}", (find(emoji,"😱😡🤬") == string::npos)); | ^^^^ not found in this scope | help: use the `.` operator to call the method `find` on `&str` | 52 - println!("find(\'😱😡🤬\') is not found: {}", (find(emoji,"😱😡🤬") == string::npos)); 52 + println!("find(\'😱😡🤬\') is not found: {}", (emoji.find("😱😡🤬") == string::npos)); | error[E0433]: failed to resolve: use of undeclared crate or module `string` --> Strings2.rs:52:67 | 52 | println!("find(\'😱😡🤬\') is not found: {}", (find(emoji,"😱😡🤬") == string::npos)); | ^^^^^^ | | | use of undeclared crate or module `string` | help: a struct with a similar name exists (notice the capitalization): `String` error[E0425]: cannot find function `find` in this scope --> Strings2.rs:53:32 | 53 | println!("find(\'bug\'): {}", find(emoji,"bug")); | ^^^^ not found in this scope | help: use the `.` operator to call the method `find` on `&str` | 53 - println!("find(\'bug\'): {}", find(emoji,"bug")); 53 + println!("find(\'bug\'): {}", emoji.find("bug")); | error[E0277]: `Option<usize>` doesn't implement `std::fmt::Display` --> Strings2.rs:53:32 | 53 | println!("find(\'bug\'): {}", find(emoji,"bug")); | ^^^^^^^^^^^^^^^^^ `Option<usize>` cannot be formatted with the default formatter | = help: the trait `std::fmt::Display` is not implemented for `Option<usize>` = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0425]: cannot find function `find` in this scope --> Strings2.rs:54:46 | 54 | println!("find(\'bug\') is not found: {}", (find(emoji,"bug") == string::npos)); | ^^^^ not found in this scope | help: use the `.` operator to call the method `find` on `&str` | 54 - println!("find(\'bug\') is not found: {}", (find(emoji,"bug") == string::npos)); 54 + println!("find(\'bug\') is not found: {}", (emoji.find("bug") == string::npos)); | error[E0433]: failed to resolve: use of undeclared crate or module `string` --> Strings2.rs:54:67 | 54 | println!("find(\'bug\') is not found: {}", (find(emoji,"bug") == string::npos)); | ^^^^^^ | | | use of undeclared crate or module `string` | help: a struct with a similar name exists (notice the capitalization): `String` error[E0425]: cannot find function `rfind` in this scope --> Strings2.rs:55:33 | 55 | println!("rfind(\'😃😇🥰\'): {}", rfind(emoji,"😃😇🥰")); | ^^^^^ not found in this scope | help: use the `.` operator to call the method `rfind` on `&str` | 55 - println!("rfind(\'😃😇🥰\'): {}", rfind(emoji,"😃😇🥰")); 55 + println!("rfind(\'😃😇🥰\'): {}", emoji.rfind("😃😇🥰")); | error[E0277]: `Option<usize>` doesn't implement `std::fmt::Display` --> Strings2.rs:55:33 | 55 | println!("rfind(\'😃😇🥰\'): {}", rfind(emoji,"😃😇🥰")); | ^^^^^^^^^^^^^^^^^^^^^ `Option<usize>` cannot be formatted with the default formatter | = help: the trait `std::fmt::Display` is not implemented for `Option<usize>` = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0425]: cannot find function `rfind` in this scope --> Strings2.rs:56:47 | 56 | println!("rfind(\'😃😇🥰\') is not found: {}", (rfind(emoji,"😃😇🥰") == string::npos)); | ^^^^^ not found in this scope | help: use the `.` operator to call the method `rfind` on `&str` | 56 - println!("rfind(\'😃😇🥰\') is not found: {}", (rfind(emoji,"😃😇🥰") == string::npos)); 56 + println!("rfind(\'😃😇🥰\') is not found: {}", (emoji.rfind("😃😇🥰") == string::npos)); | error[E0433]: failed to resolve: use of undeclared crate or module `string` --> Strings2.rs:56:69 | 56 | println!("rfind(\'😃😇🥰\') is not found: {}", (rfind(emoji,"😃😇🥰") == string::npos)); | ^^^^^^ | | | use of undeclared crate or module `string` | help: a struct with a similar name exists (notice the capitalization): `String` error[E0425]: cannot find function `rfind` in this scope --> Strings2.rs:57:33 | 57 | println!("rfind(\'bug\'): {}", rfind(emoji,"bug")); | ^^^^^ not found in this scope | help: use the `.` operator to call the method `rfind` on `&str` | 57 - println!("rfind(\'bug\'): {}", rfind(emoji,"bug")); 57 + println!("rfind(\'bug\'): {}", emoji.rfind("bug")); | error[E0277]: `Option<usize>` doesn't implement `std::fmt::Display` --> Strings2.rs:57:33 | 57 | println!("rfind(\'bug\'): {}", rfind(emoji,"bug")); | ^^^^^^^^^^^^^^^^^^ `Option<usize>` cannot be formatted with the default formatter | = help: the trait `std::fmt::Display` is not implemented for `Option<usize>` = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0425]: cannot find function `rfind` in this scope --> Strings2.rs:58:47 | 58 | println!("rfind(\'bug\') is not found: {}", (rfind(emoji,"bug") == string::npos)); | ^^^^^ not found in this scope | help: use the `.` operator to call the method `rfind` on `&str` | 58 - println!("rfind(\'bug\') is not found: {}", (rfind(emoji,"bug") == string::npos)); 58 + println!("rfind(\'bug\') is not found: {}", (emoji.rfind("bug") == string::npos)); | error[E0433]: failed to resolve: use of undeclared crate or module `string` --> Strings2.rs:58:69 | 58 | println!("rfind(\'bug\') is not found: {}", (rfind(emoji,"bug") == string::npos)); | ^^^^^^ | | | use of undeclared crate or module `string` | help: a struct with a similar name exists (notice the capitalization): `String` error: aborting due to 69 previous errors Some errors have detailed explanations: E0277, E0425, E0433. For more information about an error, try `rustc --explain E0277`.

Comparing Strings

Like numeric types, string types are comparable. This means that we can order strings and determine which ones are lower and which ones are higher in that ordering. Strings are ordered using [[Lexicographical Order]] also know as Alphabetic or Dictionary Order. This is done by comparing the first characters of the two strings to determine which string is lower and which is higher. If the first characters are the same, we move on to the second character in each string and so on until a difference is found. Shorter strings always compare lower than a longer string that has the lower string as its prefix. Like numeric types, we have six comparison operators that allow us to determine how two strings compare lexicographically.

String Comparison Operators
OperatorMeaning
==equal to
!=not equal to
<less than
<=less than or equal
>greater than
>=greater than or equal
Strings3.rs
/******************************************************************************
 * This program demonstrates string comparisons.
 * 
 * Copyright © 2020 Richard Lesh.  All rights reserved.
 *****************************************************************************/

fn main() {
	let color1:&str = "Blue";
	let color2:&str = "Red";
	let mut result:bool = false;

	println!("compare(color1, color2): {}", compareto(color1,color2));
	result = color1.to_string() < color2.to_string();
	println!("color1 < color2: {}", result);
	result = color1.to_string() > color2.to_string();
	println!("color1 > color2: {}", result);
	result = color1.to_string() == color2.to_string();
	println!("color1 == color2: {}", result);
	result = color1.to_string() != color2.to_string();
	println!("color1 != color2: {}", result);
}

Output
$ rustc Strings3.rs error[E0425]: cannot find function `compareto` in this scope --> Strings3.rs:12:42 | 12 | println!("compare(color1, color2): {}", compareto(color1,color2)); | ^^^^^^^^^ not found in this scope error: aborting due to previous error For more information about this error, try `rustc --explain E0425`.

Character Types

Often it is necessary to determine what class an individual character belongs. Character classes such as alphabetic, numeric, whitespace, control and punctuation are common classes of characters in which we would be interested. The following table illustrates how we would determine the class of a character.

Strings4.rs
/******************************************************************************
 * This program illustrates some of the string functions in Utils
 * 
 * Copyright © 2020 Richard Lesh.  All rights reserved.
 *****************************************************************************/

mod utils;

static ltrstr:&str = "   Spaces to the left";
static rtrstr:&str = "Spaces to the right  ";
static trimstr:&str = "   Spaces at each end   ";
static blank:&str = "  \t\n  ";
static lowerstr:&str = "This String is Lowercase";
static upperstr:&str = "This String is Uppercase";

fn main() {
	println!("{}{}{}", '|', Utils.ltrim(ltrstr), '|');
	println!("{}{}{}", '|', Utils.rtrim(rtrstr), '|');
	println!("{}{}{}", '|', Utils.trim(trimstr), '|');
	println!("{}{}{}", '|', Utils.trim(blank), '|');
	println!("{}{}{}", '|', tolower(lowerstr), '|');
	println!("{}{}{}", '|', toupper(upperstr), '|');
}


Output
$ rustc Strings4.rs error[E0425]: cannot find value `Utils` in this scope --> Strings4.rs:17:26 | 17 | println!("{}{}{}", '|', Utils.ltrim(ltrstr), '|'); | ^^^^^ not found in this scope error[E0425]: cannot find value `Utils` in this scope --> Strings4.rs:18:26 | 18 | println!("{}{}{}", '|', Utils.rtrim(rtrstr), '|'); | ^^^^^ not found in this scope error[E0425]: cannot find value `Utils` in this scope --> Strings4.rs:19:26 | 19 | println!("{}{}{}", '|', Utils.trim(trimstr), '|'); | ^^^^^ not found in this scope error[E0425]: cannot find value `Utils` in this scope --> Strings4.rs:20:26 | 20 | println!("{}{}{}", '|', Utils.trim(blank), '|'); | ^^^^^ not found in this scope error[E0425]: cannot find function `tolower` in this scope --> Strings4.rs:21:26 | 21 | println!("{}{}{}", '|', tolower(lowerstr), '|'); | ^^^^^^^ not found in this scope error[E0425]: cannot find function `toupper` in this scope --> Strings4.rs:22:26 | 22 | println!("{}{}{}", '|', toupper(upperstr), '|'); | ^^^^^^^ not found in this scope error: aborting due to 6 previous errors For more information about this error, try `rustc --explain E0425`.
Character Classification
FunctionDescription
isalnum(c)Check if character is alphanumeric.
isalpha(c)Check if character is alphabetic.
isblank(c)Check if character is blank.
iscntrl(c)Check if character is a control character.
isdigit(c)Check if character is decimal digit.
isgraph(c)Check if character has graphical representation.
islower(c)Check if character is lowercase letter.
isprint(c)Check if character is printable.
ispunct(c)Check if character is a punctuation character.
isspace(c)Check if character is a white-space.
isupper(c)Check if character is uppercase letter.
isxdigit(c)Check if character is hexadecimal digit.

Character Conversion
FunctionDescription
tolower(c)Returns lowercase version of the character.
toupper(c)Returns uppercase version of the character.

Questions

Projects

More ★'s indicate higher difficulty level.

References