HiveBrain v1.2.0
Get Started
← Back to all entries
patternrustCritical

Does Rust have support for functions that return multiple values?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
functionsreturnhavesupportfordoesmultiplerustthatvalues

Problem

Does Rust have native support for functions that return multiple values like Go?

func addsub(x, y int) (int, int) {
    return x + y, x - y
}


It seems that we could use a tuple to simulate it. Rosetta Code introduces how to return multiple values in different languages, but I didn't see Rust.

Solution

This works for me:

fn addsub(x: isize, y: isize) -> (isize, isize) {
    (x + y, x - y)
}


It's basically the same as in Go, but the parentheses are required.

Code Snippets

fn addsub(x: isize, y: isize) -> (isize, isize) {
    (x + y, x - y)
}

Context

Stack Overflow Q#18479648, score: 116

Revisions (0)

No revisions yet.