undefinedfix
Sign in

How does rust merge multiple structures?

Muza edited in Thu, 26 Jan 2023

Question: how to merge multiple structures into one more smoothly( Or provide the idea of structure merging ~)

Although a syntax sugar is provided, only one... Can be written at present, and there can be no comma after it

/**
* @desc merge config
*/
fn get_merge_config() -> ConfigModule {
    let default_config = get_default_config();
    let outside_config = get_outside_config();
    let merge_config=ConfigModule{
        ..default_config,// 这个例子是错误的,这里不能有逗号
        ..outside_config
    };
    println!("default config===>{:#?}", default_config);
    return merge_config;
}

JavaScript is a better operator


const a={
    aName:"a"
}
const b={
    bName="b"
}

const c= {
    ...a,
    ...b
}

console.log("==>",c)

/*
c={nName:"a",bName:"b"}
*/

Thank you very much for reading this question.

0 Replies