Strings and characters

Rust

examples/strings-and-characters/src/main.rs

macro_rules! ptr {
    ($var: expr) => {
        println!("p: {:p} ptr: {:<15?} len: {:2}, capacity: {:2} '{}'", &$var, $var.as_ptr(), $var.len(), $var.capacity(), $var)        
    };
}

fn main() {
    // Create an empty string
    let mut text = String::new();
    ptr!(text);

    // ASCII table: https://www.ascii-code.com/
    // Unicode table https://home.unicode.org/   and charts https://www.unicode.org/charts/
    // one simple character (from ASCII) is one byte
    text.push('a');
    ptr!(text);
    

    text.push('รก');
    ptr!(text);

    text.push('๐Ÿ“');
    ptr!(text);

    for byte in "๐Ÿ“".bytes() {
        println!("{byte}");
    }

    let c = String::from_utf8(vec![240, 159, 141, 147]).unwrap();
    ptr!(c);

    let pre = &text[0..3];
    println!("{pre}");

    let pre = &text[0..4];
    println!("{pre}");

}

Author

Gabor Szabo (szabgab)

Gabor Szabo, the author of the Rust Maven web site maintains several Open source projects in Rust and while he still feels he has tons of new things to learn about Rust he already offers training courses in Rust and still teaches Python, Perl, git, GitHub, GitLab, CI, and testing.

Gabor Szabo