How do I extract a number in decimal representation from a `&[u8]` and compare it with an integer?

Advertisements Here’s an example of my code: use bytes::Bytes; use regex::bytes::Regex as BytesRegex; fn main() { let val = b"2020-01-01"; let my_regex = BytesRegex::new(r"\d{4}-(\d{2})-\d{2}").unwrap(); let month = my_regex.captures(val).unwrap().get(1).unwrap(); let month_digit = std::str::from_utf8(month.as_bytes()).unwrap().parse::<u8>().unwrap(); println!("month smaller than 12: {}", month_digit<=12); } https://gist.github.com/rust-play/92d4e4855015317451abad775adceec1 Like this, I was able to print out the captured group as an unsigned integer.… Read More How do I extract a number in decimal representation from a `&[u8]` and compare it with an integer?

How can I pass a value to a function without moving ownership, just by passing the value in Rust?

Advertisements This is just example code. fn main() { let mut vec = vec![4, 3, 6, 7, 5, 8, 1, 2, 9, 0, 1]; println!("{:?}", solution(&mut vec)); } fn solution(vec: &mut std::vec::Vec<i32>) -> (i32, i32, i32) { let mut copy = vec; copy.sort(); println!("{:?}",copy); let mut len: i32 = match copy.len().try_into() { Ok(v) => v,… Read More How can I pass a value to a function without moving ownership, just by passing the value in Rust?

How to collect an iterator into a Result?

Advertisements I currently have the following function: fn filter_files<P>(input_path: P) -> Result<Vec<DirEntry>, Box<dyn error::Error>> where P: AsRef<Path> { fs::read_dir(input_path)? .into_iter() .filter_map(|res| res.ok()) .filter(|dir_entry| dir_entry.file_name().to_str().unwrap().starts_with("_x")) .collect() } Where I am attempting to filter out files that start with _x. I would like to have this function return the filtered Vec<DirEntry> directly instead of having to make… Read More How to collect an iterator into a Result?

How to apply function that returns Result to each element of HashSet in Rust

Advertisements As a language that aims for safety and performance, Rust provides a powerful data structure called HashSet that provides a fast and efficient way to store and retrieve unique values. HashSet is optimized for scenarios where you need to check for the presence of a value and avoid duplicates, making it a popular choice… Read More How to apply function that returns Result to each element of HashSet in Rust

How to apply function that returns Result to each element of HashSet in Rust?

Advertisements I have a type defined as follows: pub struct State<T: Clone + Eq> { pub db: HashSet<T>, } And I have a function that looks like the following: fn check(&self) -> Result<(), Error> { self.db.iter().for_each(|elem| elem.check()); Ok(()) } So I want to call check function for each of the element in HashSet. I just… Read More How to apply function that returns Result to each element of HashSet in Rust?

`&str` as pointing to non-contiguous portion of memory

Advertisements Is something like possible in rust? fn replace_abracadabra(s: &str) -> &str { if s.starts_with("abracadabra") { "i am static" + s.chars().last().unwrap() } else { s } } Here, "i am static" + s.chars().last().unwrap() is all borrowed, but is now a non-contiguous block of memory. >Solution : No, slices (both string and array slices) are contiguous… Read More `&str` as pointing to non-contiguous portion of memory