+ v0.1.0

Source
Joins two strings into a new one.

Package: Text

Signature

func +(self, other: String) -> String;
func +(self, other: Slice<char8>) -> String;

Parameters

NameTypeDescription
otherString / Slice<char8>The bytes to append.

Returns

A new String holding the bytes of both operands, in a block of its own. Both operands are left as they were, and joining two empty strings allocates nothing.

The slice overload lets a literal be joined on without wrapping it in a String first.

The result owns its block and has to be released with Free — including the intermediate results of a chain, which is what makes a + b + c three allocations and two leaks if only the last one is freed. In a loop it is a chain of allocations: accumulate with a StringBuilder instead.

Example

import Text::String;

func Main() -> int {
    var greeting = String::From("Hello");
    var name = String::From("Rux");

    var comma = greeting + ", "; // "Hello, "
    var full = comma + name;     // "Hello, Rux"

    full.Free();
    comma.Free();                // the intermediate owns a block too
    name.Free();
    greeting.Free();
    return 0;
}

See also

  • String — the string type
  • StringBuilder::Append — join without allocating on every step
  • Repeat — join a string to itself N times, in one allocation
  • Free — release the block the result owns