将字符串拆分为固定长度的块并在Raku中写在单独的行中

I have a file test.txt:

Stringsplittingskills

I want to read this file and write to another file out.txt with three characters in each line like

Str
ing
spl
itt
ing
ski
lls

我做了什么

my $string = "test.txt".IO.slurp;
my $start = 0;
my $elements = $string.chars;
# open file in writing mode
my $file_handle = "out.txt".IO.open: :w;
while $start < $elements {
    my $line = $string.substr($start,3);
    if $line.chars == 3 {
        $file_handle.print("$line\n") 
    } elsif $line.chars < 3 {
        $file_handle.print("$line")
    }      
    $start = $start + 3;
}
# close file handle
$file_handle.close

当字符串的长度不是3的倍数时,此方法运行良好。当字符串的长度是3的倍数时,它将在输出文件的末尾插入额外的换行符。当字符串长度为3的倍数时,如何避免在行尾插入新行?

我尝试了另一种较短的方法,

my $string = "test.txt".IO.slurp;

my $file_handle = "out.txt".IO.open: :w;
for $string.comb(3) -> $line {
    $file_handle.print("$line\n")
}

它仍然遭受相同的问题。

I looked for here, here but still unable to solve it.