Trait utils::input::ToIndex

source ·
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§

source

fn input_index(self, input: &str) -> usize

Implementations on Foreign Types§

source§

impl ToIndex for &str

source§

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 Iterators 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 &[u8]

source§

fn input_index(self, input: &str) -> usize

Find index of this subslice in the provided input.

For use with functions that iterate over a string’s bytes. See the &str implementation.

source§

impl ToIndex for char

source§

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);
source§

impl ToIndex for usize

source§

fn input_index(self, input: &str) -> usize

Index into the input string.

§Panics

This function panics if the index is out of range for the provided string.

§Examples
let string = "abcdef";
assert_eq!(4.input_index(string), 4);
let string = "abcdef";
10.input_index(string);

Implementors§