日期:2023-04-23 01:52:22 来源:博客园
(相关资料图)
match VALUE { PATTERN => EXPRESSION, PATTERN => EXPRESSION, PATTERN => EXPRESSION,}
fn main() { let favorite_color: Option<&str> = None; let is_tuesday = false; let age: Result = "34".parse(); if let Some(color) = favorite_color { println!("Using your favorite color, {}, as the background", color); } else if if_tuesday { println!("Tuesday is green day!"); } else if let Ok(age) = age { if age > 30 { println!("Using purple as the background color"); } else { println!("Using orange as the background color"); } } else { println!("Using blue as the background color"); }}
fn main() { let mut stack = Vec::new(); stack.push(1); stack.push(2); stack.push(3); while let Some(top) = stack.pop() { println!("{}", top); }}
fn main() { let v = vec!["a", "b", "c"]; for (index, value) in v.iter().enumerate() { println!("{} is at index {}", value , index); }}
fn main() { let a = 5; let (x, y, z) = (1, 2, 3); let (q, w) = (4, 5, 6); // 报错 类型不匹配 3 2}
fn foo(x: i32) { // code goes here}fn print_coordinates(&(x, y): &(i32, i32)) { println!("Current location: ({}, {})", x, y);}fn main() { let point = (3, 5); print_coordinates(&point);}
let x = 5;
if let Some(x) = a_value
fn main() { let a: Option = Some(5); let Some(x) = a: // 报错 None if let Some(x) = a {} if let x = 5 {} // 警告}
fn main() { let x = 1; match x { 1 => println!("one"), 2 => println!("two"), 3 => println!("three"), _ => println!("anything"), }}
fn main() { let x = Some(5); let y = 10; match x { Some(50) => println!("Got 50"), Some(y) => println!("Matched, y = {:?}", y), _ => println!("Default case, x = {:?}", x), } println!("at the end: x = {:?}, y = {:?}", x, y);}
fn main() { let x = 1; match x { 1 | 2 => println!("one or two"), 3 => println!("three"), _ => println!("anything"), }}
fn main() { let x = 5; match x { 1..=5 => println!("one through five"), _ => println!("something else"), } let x = "c"; match x { "a" ..="j" => println!("early ASCII letter"), "k" ..="z" => println!("late ASCII letter"), _ => println!("something else"), }}
struct Point { x: i32, y: i32,}fn main() { let p = Point { x: 0, y: 7 }; let Point { x: a, y: b } = p; assert_eq!(0, a); assert_eq!(7, b); let Point {x, y} = p; assert_eq!(0, x); assert_eq!(7, y); match p { Point {x, y: 0} => println!("On the x axis at {}", x), Point {x: 0, y} => println!("On the y axis at {}", y), Point {x, y} => println!("On neither axis: ({}, {})", x, y), }}
enum Message { Quit, Move {x: i32, y: i32}, Write(String), ChangeColor(i32, i32, i32),}fn main() { let msg = Message::ChangeColor(0, 160, 255); match msg { Message::Quit => { println!("The Quit variant has no data to destructure.") } Message::Move {x, y} => { println!("Move in the x direction {} and in the y direction {}", x, y); } Message::Write(text) => println!("Text message: {}", text), Message::ChangeColor(r, g, b) => { println!("Change the color to red {}, green {}, and blue {}", r, g, b); } }}
enum Color { Rgb(i32, i32, i32), Hsv(i32, i32, i32),}enum Message { Quit, Move {x: i32, y: i32}, Write(String), ChangeColor(Color),}fn main() { let msg = Message::ChangeColor(Color::Hsv(0, 160, 255)); match msg { Message::ChangeClolr(Color::Rgb(r, g, b)) => { println!("Change the color to red {}, green {}, and blur {}", r, g, b) } Message::ChangeColor(Color::Hsv(h, s, v)) => { println!("Change the color to hue {}, saturation {}, and value {}", h, s, v) } _ => (), }}
struct Point { x: i32, y: i32,}fn main() { let ((feet, inches), Point {x, y}) = ((3, 10), Point {x: 3, y: -10});}
fn foo(_: i32, y: i32) { println!("This code only uses the y parameter: {}", y);}fn main() { foo(3, 4);}
fn main() { let mut setting_value = Some(5); let new_setting_value = Some(10); match (setting_value, new_setting_value) { (Some(_), Some(_)) => { println!("Can"t overwrite an existing customized value"); } _ => { setting_value = new_setting_value; } } println!("setting is {:?}", setting_value); let numbers = (2, 4, 6, 8, 16, 32); match numbers { (first, _, third, _, fifth) => { println!("Some numbers: {}, {}, {}", first, third, fifth) } }}
fn main() { let _x = 5; let y = 10; // 创建未使用 警告 let s = Some(String::from("Hello")); if let Some(_s) = s { // if let Some(_) = s { println!("found a string"); } println!("{:?}", s); // 报错 }
struct Point { x: i32, y: i32, z: i32,}fn main() { let origin = Point {x: 0, y: 0, z: 0}; match origin { Point {x, ..} => println!("x is {}", x), } let numbers = (2, 4, 8, 16, 32); match numbers { (first, .., last) => { println!("Some numbers: {}, {}", first, last); } } match numbers { (.., second, ..) => { // 报错 println!("Some numbers: {}", second) }, }}
例子一:
fn main() { let num = Some(4); match num { Some(x) if x < 5 => println!("less than five: {}", x), Some(x) => println!("{}", x), None => (), }}
例子二:
fn main() { let x = Some(5); let y = 10; match x { Some(50) => println!("Got 50"), Some(n) if n == y => println!("Matched, n = {:?}", n), _ => println!("Default case, x = {:?}", x), } println!("at the end: x = {:?}, y = {:?}", x, y);}
例子三:
fn main() { let x = 4; let y = false; match x { 4 | 5 | 6 if y => println!("yes"), _ => println!("no"), }}
enum Message { Hello {id: i32},}fn main() { let msg = Message::Hello {id: 5}; match msg { Message::Hello { id: id_variable @ 3..=7, } => { println!("Found an id in range: {}", id_variable) } Message::Hello {id: 10..=12} => { println!("Found an id in another range") } Message::Hello {id} => { println!("Found some other id: {}", id) } }}
标签:
环球快播:Rust编程语言入门之模式匹配
精选!qq网名女生唯美小清新_小清新网名有哪些
当前消息!上海移动通讯费发票(上海移动通讯)
环球快报:短篇小说全集 上册
世界关注:扎哈哈迪德建筑师揭示了珠海四翼文化中心的图像
天天关注:“多彩三月三,快乐壮家娃”——凌云县加尤镇中心幼儿园开展亲子活动
每日关注!第86届山东省糖酒会开幕,参展规模提升30%,创展会历史最高
世界聚焦:国家电网在鄂最大智能装备产业园竣工
环球快看点丨拼图滑动块
今日视点:百尺竿头更进一步,逆转王艺迪晋级决赛,王曼昱再接再厉
世界热头条丨“五一”假期热门旅游城市出炉
即时焦点:中原四月春潮涌——三月初三拜轩辕
世界报道:教育部:到2025年共建设150家左右的市域产教联合体
焦点播报:得利斯董秘回复:公司总部位于山东潍坊,淄博区域市场是公司在山东省内重点打造区域
世界热门:一个完整的计算机系统应包括 _ _一个完整的计算机系统应当包括_
每日报道:“跑山电工”图书驿站,“点亮”山区孩子阅读之光
每日视讯:益阳华帝售后服务电话_华帝售后服务电话
动态焦点:油卡怎么办_具体步骤分享
天天要闻:一句“哥哥”,29万被骗了个精光!
每日聚焦:被动减持1%公司股份距离减持计划预披露日不足15个交易日,达刚控股第二大股东英奇投资收深交所监管函
最新快讯!许昌东城区:开展防汛抢险应急演练 筑牢防汛“安全墙”
全球热资讯!惠丰钻石(839725):收入快速增长,利润稳步上行,研发能力赋能长期发展-2022年年报及2023年一季报点评
当前热文:来自40万米高空的凝望!200余颗在轨遥感卫星守护“美丽中国”
天天日报丨岛城军乐飞扬
世界报道:雪佛龙锦标赛第二轮中断 金阿林领先林希妤T10
环球要闻:加拿大技术移民流程是什么
每日精选:探科学奥秘,展课堂风采
天天新资讯:我有个问题,为啥药店不许卖“安眠药”?建议放开销售~
环球热消息:如何去除粘胶痕迹(如何去除粘胶痕迹小窍门)
当前要闻:曼联名宿加里·内维尔:若阿森纳夺英超冠军 我会穿上枪手球衣