extrust/
lib.rs

1#![allow(non_upper_case_globals)]
2
3use std::error::Error;
4use std::io;
5use std::str::FromStr;
6use std::fmt::Debug;
7
8pub type Er = anyhow::Error;
9pub type Res<T> = anyhow::Result<T>;
10pub type Maybe = Res<()>;
11pub const ok: Maybe = Ok(());
12
13pub fn cin<T>() -> Res<T> 
14where T: FromStr, <T as FromStr>::Err: Error + Send + Sync + 'static
15{
16    let mut input = String::new();
17    io::stdin().read_line(&mut input)?;
18    Ok(input.trim().parse()?)
19}
20
21pub fn cout<T: Debug>(output: T){
22    println!("{output:#?}");
23}
24
25pub trait Utf8Container {
26    fn slice(&self, start: usize, end: usize) -> Option<&str>;
27    fn from(&self, start: usize) -> Option<&str>;
28    fn to(&self, end: usize) -> Option<&str>;
29}
30
31impl Utf8Container for str {
32    fn slice(&self, start: usize, end: usize) -> Option<&str> {
33        let start = self.char_indices().nth(start)?.0;
34        let end  = self.char_indices().nth(end)?.0;
35        Some(&self[start..end])
36    }
37    fn from(&self, start: usize) -> Option<&str> {
38        let start = self.char_indices().nth(start)?.0;
39        Some(&self[start..])
40    }
41    fn to(&self, end: usize) -> Option<&str> {
42        let end  = self.char_indices().nth(end)?.0;
43        Some(&self[..end])
44    }
45}