← 返回刚才阅读的位置

知识卡片

结构图卡 · 6074.a.1.c.1.a.1 · 生成模型:未记录

字符串搜索器抽象

理解等级:高级

Pattern 接收待搜索文本并生成具体 Searcher,Searcher 依次给出 SearchStep,区分匹配范围、拒绝范围和结束。Matches 迭代器委托搜索器查找匹配区间,再把区间映射为子切片。规则扩展发生在搜索器一侧,调用者沿用迭代接口。

结构图:

classDiagram
class Pattern {
    <<trait>>
    +into_searcher(haystack: &str) Searcher
}
class Searcher {
    <<trait>>
    +next() SearchStep
    +next_match() Option
}
class SearchStep {
    <<enum>>
    Match
    Reject
    Done
}
class Matches {
    +next() Option
}
class MatchesInternal {
    +next() Option
}
Pattern ..> Searcher : 生成关联类型
Searcher ..> SearchStep : 返回
Matches *-- MatchesInternal : 包装
MatchesInternal *-- Searcher : 保存
这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • 字符串与集合类型 8.1 字符串 · 查看原书依据

      在代码清单8-29中,Pattern<'a>包含了一个关联类型和四个方法。关联类型为Searcher,表示一个可以通过into_searcher方法得到的具体搜索类型,并且该搜索类型必须实现另一个Searcher<'a>trait。

    参考来源

    - 位置:《Rust编程之道》第08章《字符串与集合类型》,8.1 字符串(来源项目 book-source,源文件:`extracted/Rust编程之道/_epub-src/text/part0014_split_001.html`) - 结论依据:原文以8.1 字符串的定义或示例说明这一机制。其核心是Pattern 接收待搜索文本并生成具体 Searcher,Searcher 依次给出 SearchStep,区分匹配范围、拒绝范围和结束。 - 原始内容:在代码清单8-29中,Pattern<'a>包含了一个关联类型和四个方法。关联类型为Searcher,表示一个可以通过into_searcher方法得到的具体搜索类型,并且该搜索类型必须实现另一个Searcher<'a>trait。