`

模式匹配

    博客分类:
  • rust
 
阅读更多
fn main() {
    //if let
    let favorite_color: Option<&str> = None;//std::option::Option::Some("str");
    let is_tuesday = false;
    let age: Result<u8, _> = "34".parse();

    if let Some(color) = favorite_color {
        println!("Using your favorite color, {}, as the background", color);
    } else if is_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");
    }

    //while let
    let mut stack = Vec::new();

    stack.push(1);
    stack.push(2);
    stack.push(3);

    while let Some(top) = stack.pop() {
        println!("{}", top);
    }

    //for
    let v = vec!['a', 'b', 'c'];

    for (index, value) in v.iter().enumerate() {
        println!("{} is at index {}", value, index);
    }

    //tuple
    let point = (3, 5);
    print_coordinates(&point);

    //match
    let x = 1;

    match x {
        1 => println!("one"),
        2 => println!("two"),
        3 => println!("three"),
        _ => println!("anything"),
    }

    //match2
    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);
    //match 3
    let x = 1;

    match x {
        1 | 2 => println!("one or two"),
        3 => println!("three"),
        _ => println!("anything"),
    }

    //match 4
    let x = 5;

    match x {
        1..=5 => println!("one through five"),
        _ => println!("something else"),
    }
    //match 5
    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,
    }

    let p = Point { x: 0, y: 7 };

    let Point { x: a, y: b } = p; //let Point { x, y } = p; 一样的效果
    assert_eq!(0, a);
    assert_eq!(7, b);

    //部分匹配
    let p = Point { x: 0, y: 7 };

    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),
    }

    //枚举匹配
    // 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
    //     ),
    // }

    let msg = Message::ChangeColor(Color::Hsv(0, 160, 255));

    match msg {
        Message::ChangeColor(Color::Rgb(r, g, b)) => println!(
            "Change the color to red {}, green {}, and blue {}",
            r, g, b
        ),
        Message::ChangeColor(Color::Hsv(h, s, v)) => println!(
            "Change the color to hue {}, saturation {}, and value {}",
            h, s, v
        ),
        _ => (),
    }

    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 s = Some(String::from("Hello!"));

    if let Some(_) = s {
        println!("found a string");
    }

    println!("{:?}", s);

    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);

    //@运算符:一个保存值的@变量,将值绑定到变量 id_variable
    enum Message2 {
        Hello { id: i32 },
    }

    let msg = Message2::Hello { id: 5 };

    match msg {
        Message2::Hello {
            id: id_variable @ 3..=7,
        } => println!("Found an id in range: {}", id_variable),
        Message2::Hello { id: 10..=12 } => {
            println!("Found an id in another range")
        }
        Message2::Hello { id } => println!("Found some other id: {}", id),
    }
}

fn print_coordinates(&(x, y): &(i32, i32)) {
    println!("Current location: ({}, {})", x, y);
}

// enum Message {
//     Quit,
//     Move { x: i32, y: i32 },
//     Write(String),
//     ChangeColor(i32, i32, i32),
// }

enum Message {
    Quit,
    Move { x: i32, y: i32 },
    Write(String),
    ChangeColor(Color),
}

enum Color {
    Rgb(i32, i32, i32),
    Hsv(i32, i32, i32),
}


Using purple as the background color
3
2
1
a is at index 0
b is at index 1
c is at index 2
Current location: (3, 5)
one
Matched, y = 5
at the end: x = Some(5), y = 10
one or two
one through five
early ASCII letter
On the y axis at 7
Change the color to hue 0, saturation 160, and value 255
Can't overwrite an existing customized value
setting is Some(5)
found a string
Some("Hello!")
Default case, x = Some(5)
at the end: x = Some(5), y = 10
Found an id in range: 5

分享到:
评论

相关推荐

    串的模式匹配问题

    在计算机科学中,串(String)的模式匹配问题是一个经典且重要的问题,它涉及到字符串处理、文本搜索以及数据挖掘等领域。本程序以C语言实现,提供了高效解决串的模式匹配的方法,具有很好的实用价值。 首先,我们...

    KMP字符串模式匹配算法ppt

    KMP(Knuth-Morris-Pratt)字符串模式匹配算法是一种高效的字符串搜索算法,由D.M. Knuth、V. Morris和J.H. Pratt在1970年提出。该算法避免了简单匹配算法中的不必要的回溯,显著提高了在文本字符串中查找模式字符串...

    字符串模式匹配算法的改进研究

    ### 字符串模式匹配算法的改进研究 #### 引言 随着网络安全问题的日益突出,网络入侵检测系统(Network Intrusion Detection System, NIDS)作为一项重要的安全防护技术,在网络安全领域受到了广泛的关注。模式...

    数据结构中串的模式匹配

    模式匹配 模式匹配是数据结构中的一种重要技术,用于在一个字符串中查找另一个字符串的出现位置。在计算机科学中,模式匹配有很多实际应用,例如文本编辑、数据挖掘、信息检索等。 在模式匹配中,有两种常见的算法...

    数据结构 模式匹配的改进算法

    ### 数据结构:模式匹配的改进算法 #### 一、引言 在计算机科学领域,模式匹配是一种基础且重要的技术,广泛应用于文本处理、搜索引擎、生物信息学等多个领域。传统的模式匹配算法,如朴素的字符串匹配算法,虽然...

    模式匹配的KMP算法

    "模式匹配的KMP算法" 模式匹配的KMP算法是计算机科学领域中的一种经典算法,用于解决串的模式匹配问题。该算法可以高效地查找目标串中是否包含某个模式串,并返回模式串在目标串中的起始位置。 模式匹配的KMP算法...

    AC-BM 多模式匹配算法

    **AC-BM 多模式匹配算法** AC-BM(Aho-Corasick-BM)算法是一种结合了Aho-Corasick算法和Boyer-Moore算法的字符串匹配方法,主要用于在一个大文本串中高效地查找多个模式串。这种算法提高了在大量模式下搜索文本的...

    数据结构实验-2串模式匹配算法(串实验)

    实验二 串模式匹配算法(串实验) 实现功能:朴素的模式匹配算法(BF算法)、KMP改进算法(Next[ ])、KMP改进算法(NextVal[ ])。 主控菜单: 1.输入主串、子串和匹配起始位置 2.朴素的模式匹配算法 3.KMP改进算法...

    java版的AC多模式匹配算法

    AC多模式匹配算法 特点:应用有限自动机巧妙地将字符比较转化为了状态转移。此算法有两个特点:一是扫描文本时完全不需要回溯,二是时间复杂度为O(n)与关键字的数目和长度无关,但所需时间和文本长度以及所有关键字...

    字符串的模式匹配算法——KMP

    字符串的模式匹配算法在计算机科学中占据着重要的地位,它主要应用于文本搜索、数据分析和文本处理等领域。KMP(Knuth-Morris-Pratt)算法是其中一种高效的算法,尤其适用于处理具有重复子串的模式匹配问题。接下来...

    基于字符串模式匹配算法的病毒感染检测问题 实验四(源代码+实验报告)

    在IT领域,字符串模式匹配是数据结构和算法中一个至关重要的主题,特别是在计算机病毒检测、文本搜索、生物信息学等领域有着广泛的应用。本实验“基于字符串模式匹配算法的病毒感染检测问题”聚焦于如何利用这些算法...

    kmp模式匹配算法

    KMP(Knuth-Morris-Pratt)模式匹配算法是一种在主串(目标字符串)中查找子串(模式字符串)的高效算法,由D.E. Knuth、V.R. Morris和J.H. Pratt于1977年提出。相较于简单的暴力匹配方法,KMP算法在模式匹配过程中...

    字符串的模式匹配 数据结构 C语言

    串的模式匹配即子串定位是一种重要的串运算。设s和t是给定的两个串,在主串s中找到等于子串t的过程称为模式匹配,如果在s中找到等于t的子串,则称匹配成功,函数返回t在s中的首次出现的存储位置(或序号),否则匹配...

    PHP版的AC多模式匹配算法

    AC多模式匹配算法 特点:应用有限自动机巧妙地将字符比较转化为了状态转移。此算法有两个特点:一是扫描文本时完全不需要回溯,二是时间复杂度为O(n)与关键字的数目和长度无关,但所需时间和文本长度以及所有关键字...

    一种存储优化的多模式匹配算法

    【模式匹配】 模式匹配是计算机科学中的一种基础技术,用于在文本中寻找特定的字符串或模式。在信息检索、生物信息学、网络入侵检测等领域,模式匹配算法扮演着重要角色。多模式匹配算法,例如AC(Aho-Corasick)...

    字符串-模式匹配

    在这个主题中,我们将深入探讨两种常见的模式匹配算法:一般模式匹配和KMP(Knuth-Morris-Pratt)模式匹配,同时也会涉及C语言实现的相关知识。 1. 一般模式匹配: 一般模式匹配是最基础的字符串匹配方法,通常采用...

    Wu-Manber多模式匹配算法源码

    Wu-Manber多模式匹配算法是一种高效且实用的字符串搜索算法,尤其在处理大量模式字符串时表现优秀。该算法由Michael Wu和Steven Manber于1992年提出,其主要目的是解决在一个大文本中同时查找多个模式字符串的问题。...

    多模式匹配算法

    ### 多模式匹配算法在中英文混合环境中的应用 #### 摘要解析与理论背景 本文探讨了一种专门针对中英文混合文本的多模式匹配算法。多模式匹配算法是计算机科学领域的一种基本技术,主要用于查找一个或多个模式在...

    串的简单模式匹配(算法)

    串的简单模式匹配算法 串的简单模式匹配算法是计算机科学中的一种基本算法,用于在主串中查找模式串的出现位置。该算法的实现主要基于单链表的数据结构,通过遍历单链表来实现模式匹配。 单链表结构体的定义 -----...

    基于字符串模式匹配算法的病毒感染检测问题_算法_数据结构_

    字符串模式匹配算法在此过程中扮演了核心角色,因为它可以高效地查找可能的病毒签名或恶意代码序列。本话题将深入探讨如何利用C语言实现基于字符串模式匹配算法的病毒感染检测。 首先,我们需要了解字符串模式匹配...

Global site tag (gtag.js) - Google Analytics