Skip to content

replace ​

Firmareplace<T>(destination: T, source: T): T

replace almacena source en un binding local mutable y devuelve el valor que estaba almacenado antes. Admite tipos Copy y string. Para string, los dos argumentos deben ser bindings locales mutables; source se mueve y no puede usarse después de la llamada.

tyrus
import { replace } from "tyrus:memory";

function main(): void {
    let value: i32 = 4;
    const previous: i32 = replace(value, 12);
    print(value); // 12
    print(previous); // 4

    let source: string = "new value";
    let destination: string = "old value";
    const oldText: string = replace(destination, source);
    print(destination); // new value
    print(oldText); // old value
}

error

tyrus
import { replace } from "tyrus:memory";

function main(): void {
    let source: string = "new value";
    let destination: string = "old value";
    const oldText: string = replace(destination, source);
    print(source); 
    print(destination);
    print(oldText);
}
text
error[E0382]: use of moved value: `source`
at /src/main.tyr:7:11
    print(source);
          ^^^^^^
at /src/main.tyr:6:50
    const oldText: string = replace(destination, source);
                                                 ^^^^^^ value moved here; borrow `source` instead if it must remain usable
help: borrow `source` at the move site if it must remain usable later
help: for example: `const oldText: &string = &source;`