String Parsing

Collect Unicode Graphemes

unicode-segmentationunicode-segmentation-crates.iounicode-segmentation-githubunicode-segmentation-lib.rs cat-text-processing

Collect individual Unicode graphemes from UTF-8 string using the unicode_segmentation::UnicodeSegmentation::graphemes⮳ function from the unicode_segmentation⮳ crate.

use unicode_segmentation::UnicodeSegmentation;

fn main() {
    let name = "José Guimarães\r\n";
    let graphemes =
        UnicodeSegmentation::graphemes(name, true).collect::<Vec<&str>>();
    assert_eq!(graphemes[3], "é");
}

Implement the FromStr trait for a custom struct

std cat-text-processing

Creates a custom struct RGB and implements the FromStr trait to convert a provided color hex code into its RGB color code.

use std::str::FromStr;

#[derive(Debug, PartialEq)]
struct Rgb {
    r: u8,
    g: u8,
    b: u8,
}

impl FromStr for Rgb {
    type Err = std::num::ParseIntError;

    // Parses a color hex code of the form '#rRgGbB..' into an
    // instance of 'RGB'
    fn from_str(hex_code: &str) -> Result<Self, Self::Err> {
        // u8::from_str_radix(src: &str, radix: u32) converts a string
        // slice in a given base to u8
        let r: u8 = u8::from_str_radix(&hex_code[1..3], 16)?;
        let g: u8 = u8::from_str_radix(&hex_code[3..5], 16)?;
        let b: u8 = u8::from_str_radix(&hex_code[5..7], 16)?;

        Ok(Rgb { r, g, b })
    }
}

fn main() {
    let code: &str = r"#fa7268";
    match Rgb::from_str(code) {
        Ok(rgb) => {
            println!(
                r"The RGB color code is: R: {} G: {} B: {}",
                rgb.r, rgb.g, rgb.b
            );
        }
        Err(_) => {
            println!("{} is not a valid color hex code!", code);
        }
    }

    // test whether from_str performs as expected
    assert_eq!(
        Rgb::from_str(r"#fa7268").unwrap(),
        Rgb {
            r: 250,
            g: 114,
            b: 104
        }
    );
}