pub trait ToIndex {
// Required method
fn input_index(self, input: &str) -> usize;
}
Expand description
Helper trait to simplify error location tracking.
Used in InputError::new
.
Required Methods§
fn input_index(self, input: &str) -> usize
Implementations on Foreign Types§
source§impl ToIndex for &str
impl ToIndex for &str
source§fn input_index(self, input: &str) -> usize
fn input_index(self, input: &str) -> usize
Find index of this substring in the provided input.
Uses the pointer offset, meaning it works if this substring is not the first occurrence in
the string. This allows recovering the error position without tracking an offset into the
string, which is useful when using Iterator
s such as str::lines
on an input.
§Panics
This function panics if this string is not a substring inside the provided string.
§Examples
let string = "abcabc";
assert_eq!(string[4..].input_index(string), 4);
ⓘ
let string = "abcabc";
let mut other = String::new();
other.push('b');
other.push('c');
other.input_index(string);
source§impl ToIndex for char
impl ToIndex for char
source§fn input_index(self, input: &str) -> usize
fn input_index(self, input: &str) -> usize
Find the first instance of this character in the string.
Intended for puzzles where the entire input should be a certain set of characters, so if an invalid character is found, the instance in the error doesn’t matter.
§Panics
This function panics if this character is not present in the string
§Examples
let string = "abca bc";
assert_eq!(' '.input_index(string), 4);
ⓘ
let string = "abcdef";
' '.input_index(string);