Skip to content

Generic swap

editable example

Click the pencil to open this in an editor, change it, and run it in your browser. The same solution is posted on Rosetta Code.

ghul
use IO.Std.write_line
swap[T](left: T ref, right: T ref) is
(left!, right!) = (right!, left!)
si
let a mut = 1
let b mut = 2
swap(a ref, b ref)
write_line("ints: {a} {b}")
let s mut = "first"
let t mut = "second"
swap(s ref, t ref)
write_line("strings: {s} {t}")
let p mut = (1, "one")
let q mut = (2, "two")
swap(p ref, q ref)
write_line("tuples: {p} {q}")