str::starts_with¶
Level: reference · for working programmers
One line: true if the string begins with the pattern — anchored, so it costs the length of the prefix rather than a scan.
Stable since 1.0.0.
Takes the same pattern shapes as everything else in the family, so s.starts_with(char::is_numeric) asks whether the first character is a digit — a predicate pattern is tested against one character here, not against the whole string.
When you are going to remove the prefix afterwards, strip_prefix does both in one step and cannot get the length wrong:
fn main() {
let flag = "--verbose";
// starts_with then slice: the 2 has to match the "--" by hand
if flag.starts_with("--") { println!("{}", &flag[2..]); }
// strip_prefix: no arithmetic to get wrong
if let Some(name) = flag.strip_prefix("--") { println!("{name}"); }
}
The byte-arithmetic version is where a multi-byte prefix goes wrong: "é".len() is 2, so a hand-written &s[1..] panics.
Every string starts with "".
Example¶
str_starts_with.rs in full — pasted here by tools/run_examples.py from the file CI compiles and runs.
fn main() {
let path = "/usr/local/bin";
println!("{}", path.starts_with('/'));
println!("{}", path.starts_with("/usr"));
println!("{}", path.starts_with(char::is_alphabetic));
// A predicate tests the first character only.
println!("{}", "7up".starts_with(char::is_numeric));
// The pairing that avoids hand-written byte arithmetic.
for flag in ["--verbose", "-v", "plain"] {
match flag.strip_prefix("--") {
Some(name) => println!("{flag:<10} long option {name:?}"),
None => println!("{flag:<10} starts_with(\"--\") = {}", flag.starts_with("--")),
}
}
// Everything starts with the empty pattern.
println!("{}", path.starts_with(""));
}
Verified output of str_starts_with.rs — regenerated by tools/run_examples.py, never hand-typed.
true
true
false
true
--verbose long option "verbose"
-v starts_with("--") = false
plain starts_with("--") = false
true
See also¶
str::ends_with— the same test at the other endstr::strip_prefix— test and remove in one stepstr::contains— anywhere rather than at the frontstr::find— where the pattern is
str::starts_with in the standard library ↗
Po polsku¶
starts_with jest zakotwiczone na początku tekstu, więc kosztuje tyle, ile ma prefiks, a nie tyle, ile ma cały łańcuch znaków. Osobno zapamiętaj zachowanie wzorca predykatowego (predicate pattern): "7up".starts_with(char::is_numeric) pyta wyłącznie o pierwszy znak, a nie o cały tekst — dlatego "/usr/local/bin".starts_with(char::is_alphabetic) zwraca false. Największa praktyczna pułapka zaczyna się dopiero wtedy, gdy po udanym teście chcesz prefiks odciąć, bo liczba, którą wpisujesz w wycinek, liczy bajty, a nie znaki: "Święto" przechodzi starts_with("Ś"), ale &s[1..] na nim panikuje, skoro Ś zajmuje dwa bajty. Dlatego parę „sprawdź i utnij” pisze się jako strip_prefix("Ś") — jeden krok, zero arytmetyki i żadnej okazji do pomyłki; przy okazji warto wiedzieć, że każdy tekst zaczyna się od pustego wzorca "".
Szukaj po polsku: sprawdzanie prefiksu tekstu · odcinanie przedrostka bez paniki · rust starts_with strip_prefix · rust byte index is not a char boundary