rust学习——变量遮蔽特性(继承式可变)

编程入门 行业动态 更新时间:2024-10-26 17:25:37

rust学习——<a href=https://www.elefans.com/category/jswz/34/1771380.html style=变量遮蔽特性(继承式可变)"/>

rust学习——变量遮蔽特性(继承式可变)

遮蔽

声明和前面变量具有相同名称的新变量

例子1

fn main() {let x = 5;let x = x + 1;{let x = x * 2;println!("The value of x in the inner scope is: {}", x);}println!("The value of x is: {}", x);
}

运行

$ cargo runCompiling variables v0.1.0 (file:///projects/variables)Finished dev [unoptimized + debuginfo] target(s) in 0.31sRunning `target/debug/variables`
The value of x in the inner scope is: 12
The value of x is: 6

例子2

let c = Context::new();
let c = c.rot_deg(90.0);
let transform = c.transform;let c = Context::new();
let c = c.scale(2.0, 3.0); 
let transform = c.transform;

上面代码用同一个变量 c,持有上下文,利用变量遮蔽,调用不同的方法,返回不同的类型,在代码维护可读性上是非常有帮助的。(别说人的,不是我说的。)

更多推荐

rust学习——变量遮蔽特性(继承式可变)

本文发布于:2023-12-08 03:56:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1672283.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   特性   rust

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!