It seems that you need "fragments":
#![feature(slice_patterns)] fn main() { let line = "127.0.0.1 1000 what!?"; let v = line.split_whitespace().take(3).collect::<Vec<&str>>(); if let [ip, port, msg] = &v[..] { println!("{}:{} says '{}'", ip, port, msg); } }
Pay attention to if let instead of the usual let . Slice patterns are rebuttable, so we need to consider this (you might want to have an else branch).
This will require a nightly version of Rust.
source share