ads2_2022/code/rust/src/core/utils.rs

56 lines
1.4 KiB
Rust

// ----------------------------------------------------------------
// IMPORTS
// ----------------------------------------------------------------
extern crate regex;
use self::regex::Regex;
// ----------------------------------------------------------------
// METHODS get regex
// ----------------------------------------------------------------
/// Constructs RegEx and panics if error.
#[allow(dead_code)]
pub fn construct_regex(pattern: &str) -> Regex {
return Regex::new(pattern)
.expect("Invalid regex construction!");
}
// ----------------------------------------------------------------
// METHODS values
// ----------------------------------------------------------------
pub fn min<T>(x: T, y: T) -> T
where T: PartialOrd
{
return if y < x { y } else { x };
}
// ----------------------------------------------------------------
// METHODS Vectors
// ----------------------------------------------------------------
pub fn restrict<T>(x: &mut Vec<T>, i: usize, j: usize) -> Vec<T>
where T: Clone
{
return x[i..j].iter()
.cloned()
.collect::<Vec<T>>();
}
pub fn remove_last<T>(x: &mut Vec<T>) -> Vec<T>
where T: Clone
{
let n = x.len();
return restrict::<T>(x, 0, n-1);
}
#[allow(dead_code)]
pub fn remove_first<T>(x: &mut Vec<T>) -> Vec<T>
where T: Clone
{
let n = x.len();
return restrict::<T>(x, 0, n);
}