← 返回起始笔记位置

知识卡片

普通读书笔记卡 · 6074.f.9.a.1 · 生成模型:未记录

搜索结果借自输入正文

理解等级:中级

签名 search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> 把切片有效期系在正文上。query 的借用与输出寿命独立。省略标注时两个输入具有各自的借用,编译器无法判断返回切片来自哪个输入。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第12章 I/O 项目:构建命令行程序(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c12.xhtml`,块 0195) - 结论依据:原文明确输出向量的切片来自 `contents`。 - 原始内容:Notice that we need to define an explicit lifetime 'a in the signature of search and use that lifetime with the contents argument and the return value. Recall in Chapter 10 that the lifetime parameters specify which argument lifetime is connected to the lifetime of the return value.