You can build some syntactic sugar `..=` that allows to unpack sequences and
arrays ([implementation in this SO
answer](https://stackoverflow.com/a/31975748/4178189)). With that you can do
(taking also advantage of `maxsplit` parameter):
var a, b, rest: string
(a, b, rest) ..= "a b rest of the alphabet...".split(maxsplit=2)
echo a, b # ab
echo rest # rest of the alphabet...
# in case you are not gonna use the rest
(a, b) ..= "a b rest of the alphabet...".split(maxsplit=2)
echo a, b # ab
Run
Note that rest is a string and not a seq[string]. You can adapt the syntactic
sugar above (here I call it `...=` with 3 dots) and have:
var restAsSeq: seq[string]
(a, b, restAsSeq) ...= "a b rest of the alphabet...".split() # this does
return tail of string and discards it
echo a, b
echo restAsSeq # @["rest", "of", "the", "alphabet..."]
Run
In case you do not need to use the `rest`, note that `split` proc does still
allocate and return the rest of the string and it is the macro that throws away
the stuff not needed (in particular the above would work the same without
maxsplit).
A more efficient solution in case you have a very long string and you want to
avoid even the allocation could be:
proc first(s: string, n: int): seq[string] =
var i = 0
for e in split(s): # this uses split iterator and only parses what it
needs
result.add e
inc i
if i >= n:
break
(a, b) ..= "a b c d e f".first(2)
echo a, b
Run
[Full example in playground](https://play.nim-lang.org/#ix=2DMx)