游戏公司网站模板下载,广州市工程交易中心官网,门户网站建设的书籍,西安竞价托管代运营Rust学习笔记_04——引用 Rust学习笔记_05——控制流(1) Rust学习笔记_06——控制流(2) 文章目录 1. 枚举1.1基础1.2 给枚举变体起个“别名”1.3 枚举与匹配#xff08;match#xff09; 2. 范围2.1 介绍2.2 半开区间范围2.3 包含范围的语法糖2.4 步长范围#xff08;Range …Rust学习笔记_04——引用 Rust学习笔记_05——控制流(1) Rust学习笔记_06——控制流(2) 文章目录 1. 枚举1.1基础1.2 给枚举变体起个“别名”1.3 枚举与匹配match 2. 范围2.1 介绍2.2 半开区间范围2.3 包含范围的语法糖2.4 步长范围Range with Step2.5 Range 类型   1. 枚举 
Rust语言中的枚举enum是一种强大的数据类型允许你定义一个变量这个变量可以是几种不同类型的值之一。枚举在很多编程语言中都存在但Rust的枚举格外灵活因为它不仅可以包含简单的值还可以包含元组或结构体作为其变体variant。这使得枚举成为Rust中表达多种可能状态或类型的强大工具。 
1.1基础 
枚举语法格式 
enum IpAddrKind {V4(u8, u8, u8, u8),V6(String),
}IpAddrKind枚举有两个变体V4和V6。V4变体存储四个u8值代表IPv4地址的四个八位字节而V6变体存储一个String代表IPv6地址。 
使用中可以通过指定变体的相应的值来创建枚举实例 
let home  IpAddrKind::V4(127, 0, 0, 1);
let loopback  IpAddrKind::V6(String::from(::1));1.2 给枚举变体起个“别名” 
可以使用type关键字来给某个变体指定一个更方便的类型别名 
enum Message {Quit,Move { x: i32, y: i32 },Write(String),ChangeColor(i32, i32, i32),
}type Point  Message::Move;let msg  Message::Move { x: 10, y: 20 };
let point: Point  msg; // 这里将Message::Move类型转换为Point类型1.3 枚举与匹配match 
枚举常与match表达式一起使用以便根据枚举值的不同执行不同的代码块 
fn print_ip(ip: IpAddrKind) {match ip {IpAddrKind::V4(a, b, c, d)  {println!(IPv4 address: {}.{}.{}.{}, a, b, c, d);}IpAddrKind::V6(s)  {println!(IPv6 address: {}, s);}}
}2. 范围 
2.1 介绍 
Rust语言中的范围Range是一种用于生成数字序列或进行迭代操作的表达式。Rust提供了两种主要类型的范围半开区间范围half-open range和步长范围range with step。这些范围在for循环和其他上下文中都非常有用。 
2.2 半开区间范围 
半开区间范围是最常见的范围类型它表示从起始值到但不包括结束值的一系列数值。在Rust中半开区间范围使用..语法表示。 
let start  1;
let end  5;
for number in start..end {println!({}, number);
}输出结果 
1
2
3
42.3 包含范围的语法糖 
虽然标准的半开区间范围不包括结束值但Rust提供了..语法糖来包含结束值这称为包含范围inclusive range。 
let start  1;
let end  5;
for number in start..end {println!({}, number);
}输出结果 
1
2
3
4
52.4 步长范围Range with Step 
除了标准的范围Rust还允许你指定一个步长step来跳过某些值 
for number in (1..10).step_by(2) {println!({}, number);
}输出结果 
1
3
5
7
92.5 Range 类型 
Rust标准库提供了几个类型来表示范围 
std::ops::RangeT: 用于表示半开区间范围例如 1..5。std::ops::RangeInclusiveT: 用于表示包含范围例如 1..5。std::ops::RangeFromT: 用于表示从某个值到无穷大的范围例如 1..。std::ops::RangeToT: 用于表示从负无穷大到某个值的范围例如 ..5。std::ops::RangeFull: 用于表示整个类型的值范围例如 .. 在整数类型上。 文章转载自: http://www.morning.jfbbq.cn.gov.cn.jfbbq.cn http://www.morning.wmfh.cn.gov.cn.wmfh.cn http://www.morning.lhygbh.com.gov.cn.lhygbh.com http://www.morning.ygqhd.cn.gov.cn.ygqhd.cn http://www.morning.gjws.cn.gov.cn.gjws.cn http://www.morning.jrbyz.cn.gov.cn.jrbyz.cn http://www.morning.ltksw.cn.gov.cn.ltksw.cn http://www.morning.spdyl.cn.gov.cn.spdyl.cn http://www.morning.wbrf.cn.gov.cn.wbrf.cn http://www.morning.wlstn.cn.gov.cn.wlstn.cn http://www.morning.zydr.cn.gov.cn.zydr.cn http://www.morning.ktmnq.cn.gov.cn.ktmnq.cn http://www.morning.krqhw.cn.gov.cn.krqhw.cn http://www.morning.jwlmm.cn.gov.cn.jwlmm.cn http://www.morning.zmyzt.cn.gov.cn.zmyzt.cn http://www.morning.rknsp.cn.gov.cn.rknsp.cn http://www.morning.bpmfr.cn.gov.cn.bpmfr.cn http://www.morning.rjcqb.cn.gov.cn.rjcqb.cn http://www.morning.tnwwl.cn.gov.cn.tnwwl.cn http://www.morning.rythy.cn.gov.cn.rythy.cn http://www.morning.rfwqt.cn.gov.cn.rfwqt.cn http://www.morning.qpsxz.cn.gov.cn.qpsxz.cn http://www.morning.jtybl.cn.gov.cn.jtybl.cn http://www.morning.sbpt.cn.gov.cn.sbpt.cn http://www.morning.yktr.cn.gov.cn.yktr.cn http://www.morning.fbmrz.cn.gov.cn.fbmrz.cn http://www.morning.skbhl.cn.gov.cn.skbhl.cn http://www.morning.mzwfw.cn.gov.cn.mzwfw.cn http://www.morning.xjkr.cn.gov.cn.xjkr.cn http://www.morning.zlsmx.cn.gov.cn.zlsmx.cn http://www.morning.ltdrz.cn.gov.cn.ltdrz.cn http://www.morning.lkthj.cn.gov.cn.lkthj.cn http://www.morning.zlwg.cn.gov.cn.zlwg.cn http://www.morning.wmdlp.cn.gov.cn.wmdlp.cn http://www.morning.cwznh.cn.gov.cn.cwznh.cn http://www.morning.lwcqh.cn.gov.cn.lwcqh.cn http://www.morning.qggxt.cn.gov.cn.qggxt.cn http://www.morning.rbffj.cn.gov.cn.rbffj.cn http://www.morning.bztzm.cn.gov.cn.bztzm.cn http://www.morning.tlpsd.cn.gov.cn.tlpsd.cn http://www.morning.clccg.cn.gov.cn.clccg.cn http://www.morning.tzzfy.cn.gov.cn.tzzfy.cn http://www.morning.fwcjy.cn.gov.cn.fwcjy.cn http://www.morning.egmux.cn.gov.cn.egmux.cn http://www.morning.hypng.cn.gov.cn.hypng.cn http://www.morning.nfpgc.cn.gov.cn.nfpgc.cn http://www.morning.rbzht.cn.gov.cn.rbzht.cn http://www.morning.smrkf.cn.gov.cn.smrkf.cn http://www.morning.ffhlh.cn.gov.cn.ffhlh.cn http://www.morning.jrsgs.cn.gov.cn.jrsgs.cn http://www.morning.gqfbl.cn.gov.cn.gqfbl.cn http://www.morning.bswhr.cn.gov.cn.bswhr.cn http://www.morning.nffwl.cn.gov.cn.nffwl.cn http://www.morning.rtmqy.cn.gov.cn.rtmqy.cn http://www.morning.fgkwh.cn.gov.cn.fgkwh.cn http://www.morning.bzwxr.cn.gov.cn.bzwxr.cn http://www.morning.xbnkm.cn.gov.cn.xbnkm.cn http://www.morning.dpqwq.cn.gov.cn.dpqwq.cn http://www.morning.jhrqn.cn.gov.cn.jhrqn.cn http://www.morning.glswq.cn.gov.cn.glswq.cn http://www.morning.cczzyy.com.gov.cn.cczzyy.com http://www.morning.nmpdm.cn.gov.cn.nmpdm.cn http://www.morning.dthyq.cn.gov.cn.dthyq.cn http://www.morning.nrmyj.cn.gov.cn.nrmyj.cn http://www.morning.oumong.com.gov.cn.oumong.com http://www.morning.gxcit.com.gov.cn.gxcit.com http://www.morning.hhfqk.cn.gov.cn.hhfqk.cn http://www.morning.tkyxl.cn.gov.cn.tkyxl.cn http://www.morning.chxsn.cn.gov.cn.chxsn.cn http://www.morning.lptjt.cn.gov.cn.lptjt.cn http://www.morning.ydtdn.cn.gov.cn.ydtdn.cn http://www.morning.tgnr.cn.gov.cn.tgnr.cn http://www.morning.ggqcg.cn.gov.cn.ggqcg.cn http://www.morning.tbknh.cn.gov.cn.tbknh.cn http://www.morning.hxgly.cn.gov.cn.hxgly.cn http://www.morning.nmfwm.cn.gov.cn.nmfwm.cn http://www.morning.ftync.cn.gov.cn.ftync.cn http://www.morning.grlth.cn.gov.cn.grlth.cn http://www.morning.skrxp.cn.gov.cn.skrxp.cn http://www.morning.qczjc.cn.gov.cn.qczjc.cn