Hacker News new | past | comments | ask | show | jobs | submit login

Rust version:

    fn main()
    {
        if let Some(arg) = std::env::args().nth(1)
        {
            println!("{}", arg.chars().take(12).collect::<String>()); // chars() iteraters over codepoints
        }
    }



Idiomatic Rust would probably avoid allocations, which means something more like

    fn main() {
        if let Some(arg) = std::env::args().nth(1) {
            println!("{}", {
                match arg.char_indices().nth(12) {
                    Some((idx, _)) => &arg[..idx],
                    None => &*arg
                }
            });
        }
    }
With the `unicode-segmentation` crate[1], you can just swap `char_indices()` with `grapheme_indices(true)`.

[1] https://crates.io/crates/unicode-segmentation




Consider applying for YC's W25 batch! Applications are open till Nov 12.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: