官术网_书友最值得收藏!

Shadowing

Instead of changing variable properties, it's often more readable to overwrite a variable with a different value (for example, a changed copy of the original). This technique is called shadowing.

Typically, this is used to reuse a variable name, even though the actual value has changed, to work in the current situation. This snippet sanitizes String and, by using the same name throughout the function, it's always clear that it's the input parameter that is changed:

fn sanitize(s: String) -> String {
let s = s.trim();
let s = s.replace(" ", "_");
s
}

While this is akin to changing the value of a variable, shadowing does not replace mutability, especially when it's less costly to actually change properties of that variable; Rust has a specific design pattern for that!

主站蜘蛛池模板: 广宗县| 叙永县| 乌拉特后旗| 东城区| 武威市| 邛崃市| 江达县| 临西县| 微山县| 门头沟区| 揭东县| 孝感市| 罗江县| 永和县| 阿合奇县| 宿松县| 即墨市| 普兰县| 潮州市| 普宁市| 清流县| 滦南县| 洪洞县| 綦江县| 格尔木市| 三穗县| 喀喇沁旗| 榆树市| 彭水| 弥勒县| 乐山市| 望谟县| 军事| 崇明县| 兴业县| 宜城市| 报价| 晋宁县| 五原县| 眉山市| 阜宁县|