Library reference

Match the start of a string

Check the letters at the start of the text. The result is true only when those letters match.

str::starts_with

Simple example

Does abc start with a?

Start'abc' · 'a'
Resulttrue
main.rs
fn main() {
    println!("{}", "abc".starts_with("a"));
}
Output
true
Advertisements

Everyday examples

Child tickets start with C-. Is this a child ticket?

Start'C-18' · 'C-'
Resulttrue
main.rs
fn main() {
    println!("{}", "C-18".starts_with("C-"));
}
Output
true

Express parcels start with EX. Should this parcel go in the express tray?

Start'BX42' · 'EX'
Resultfalse
main.rs
fn main() {
    println!("{}", "BX42".starts_with("EX"));
}
Output
false

Is this shelf in the LAB section?

Start'LAB-3' · 'LAB'
Resulttrue
main.rs
fn main() {
    println!("{}", "LAB-3".starts_with("LAB"));
}
Output
true
Run the example
Terminal
rustc main.rs
./main
Official documentation