小编Ham*_*tar的帖子

从 Rust 中的函数返回值有哪些不同的方法?

我遇到了以下两种方式:

#[derive(Debug)]
struct InputList(i32, i32, i32, i32);
#[derive(Debug)]
struct OutputList(i32, i32, i32, i32);

// Option 1
fn foo(input_list: InputList) -> OutputList {
    return OutputList(input_list.0, input_list.1, input_list.2, input_list.3);
}

// Option 2
fn bar(input_list: InputList) -> OutputList {
    OutputList(input_list.0, input_list.1, input_list.2, input_list.3)
}

fn main() {
    let input_list1 = InputList(1, 2, 3, 4);
    let input_list2 = InputList(6, 7, 8, 9);

    println!("foo() invocation output: {:?}", foo(input_list1));
    println!("bar() invocation output: {:?}", bar(input_list2));
}
Run Code Online (Sandbox Code Playgroud)

只有这两种选择吗?

return function rust

0
推荐指数
1
解决办法
103
查看次数

标签 统计

function ×1

return ×1

rust ×1