home ~ projects ~ socials

Remove Characters From The End Of A String In Rust

fn main() {
    let source = "alfabravocharlie";
    let chopped = source.get(0..4).unwrap();
    println!("{}", chopped);
}
Output:
alfa

Notes

  • This is really making a new string with just the selected characters from the beining of the source string, but that's too long for a title
  • I genearlly think of this as truncating, but that means completely deleting in some contexts
  • This is like a substring in some other langauges. Should probably rename the title to that at some point
-- end of line --