Return

Attribute Macro Return 

Source
#[Return]
Expand description

We usually return more than 1 values from a function. In such situations, Rust supports only tupple as a way to bundle returned values. But it’s sometimes a bit anoying: when we’d like to name freely to each field, not 0, 1, 2, …

#[Return] enables this naming. You can write functions like

use atruct::Return;
 
fn main() {
    let abc = get_abc();
    println!("{}", abc.a);  // 24
    println!("{}", abc.b);  // you can use any type for a field
    println!("{:?}", abc.c);  // [-1, 0, 0, -1, 1, 0, 1, -1]
}
 
#[Return(a: u8, b: String, c: Vec<isize>)]  // not supporting nest
fn get_abc() {
    Return {
        a: 24,
        b: "you can use any type in a field".into(),
        c: vec![-1,0,0,-1,1,0,1,-1],
    }
}

( examples/return_struct.rs )


  • Unlike atruct!, #[Return] doesn’t support nested structs. So returned value is just like a tupple you can give any names to its fields.
  • #[Return] automatically generates a struct named as “FunctionName” ( e.g. if function is get_abc, for example, GetAbc ), But at the same time defines a type synonym Return. So you DON’t need to memorize the generated struct’s name.