AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 78565781
Accepted
Olivier Lasne
Olivier Lasne
Asked: 2024-06-02 15:55:42 +0800 CST2024-06-02 15:55:42 +0800 CST 2024-06-02 15:55:42 +0800 CST

当所有参数都实现复制特征时,为什么这个函数需要生命周期?

  • 772

在下面的代码中,我不明白为什么 Span需要 life。由于所有参数都实现了复制特征,我的理解是 Span 拥有所有必需的变量。

use ratatui::{
    style::{Color, Style},
    text::Span,
};

/// Take a u8, and render a colorized ascii, or placeholdler
fn render_ascii_char(val: u8) -> Span {
    match val {

        val if val > 0x20 && val < 0x7f => {
            Span::styled(
                val.to_string(),
                Style::default().fg(Color::LightCyan)
            )
        },

        _ => {
            Span::styled(
                "•",
                Style::default().fg(Color::Yellow)
            )
        }
    }
}

fn test_function() -> Span {
    let val = 0x42;

    render_ascii_char(val)
}

fn main() {
    let _my_span = test_function();
}

这段代码使用ratatui.波纹管Cargo.toml:

[package]
name = "span_lifetime"
version = "0.1.0"
edition = "2024"

[dependencies]
crossterm = "0.27.0"
ratatui = "0.26.2"

编译时出现以下错误消息cargo build:

$ cargo build
   Compiling span_lifetime v0.1.0 (/tmp/playground/span_lifetime)
error[E0106]: missing lifetime specifier
 --> src/main.rs:7:34
  |
7 | fn render_ascii_char(val: u8) -> Span {
  |                                  ^^^^ expected named lifetime parameter
  |
  = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
help: consider using the `'static` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static`, or if you will only have owned values
  |
7 | fn render_ascii_char(val: u8) -> Span<'static> {
  |                                      +++++++++

error[E0106]: missing lifetime specifier
  --> src/main.rs:26:23
   |
26 | fn test_function() -> Span {
   |                       ^^^^ expected named lifetime parameter
   |
   = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
help: consider using the `'static` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static`, or if you will only have owned values
   |
26 | fn test_function() -> Span<'static> {
   |                           +++++++++

For more information about this error, try `rustc --explain E0106`.
error: could not compile `span_lifetime` (bin "span_lifetime") due to 2 previous errors

使用<'static>生命周期似乎不是一个好主意,因为我不只将此函数与常量一起使用。

使用引用val: &u8作为参数,而不是val: u8确实有效。但当传递的值超出范围时,就会产生问题。

到目前为止我的解决方案是指定生命周期:

fn render_ascii_char<'a>(val: u8) -> Span<'a> {
    match val {
[...]

但我真的不明白为什么需要这样做,也不明白这一生的含义。

rust
  • 1 1 个回答
  • 54 Views

1 个回答

  • Voted
  1. Best Answer
    cafce25
    2024-06-02T16:10:40+08:002024-06-02T16:10:40+08:00

    根据 的定义Span:

    pub struct Span<'a> {
        pub content: Cow<'a, str>,
        pub style: Style,
    }
    

    您可以看到它包含一个,Cow其中可能包含变体中的引用Borrowed:

    pub enum Cow<'a, B>
    where
        B: 'a + ToOwned + ?Sized,
    {
        Borrowed(&'a B),
        Owned(<B as ToOwned>::Owned),
    }
    

    因此,结构本身需要用生命周期进行注释,因为您无法动态添加生命周期(这本质上是不可能的,因为类型和生命周期只是编译时构造!)。

    由于您不满足任何可以省略生命周期的条件,因此您必须显式添加它。

    'static是在这里使用的正确生命周期,它根本不意味着您可以使用或只能使用Span常量返回的值,而是它不包含任何生命周期短于 的引用'static。

    • 2

相关问题

  • 在匹配内重用函数时,匹配臂具有预期的不兼容类型

  • match 语句中的 Rust 类型转换

  • 如何强制匹配的返回类型为()?

  • 原始表示中的 Rust 枚举

  • 有没有办法直接简化 Result<String, VarError> 中 Ok("VAL") 的匹配

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行?

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    何时应使用 std::inplace_vector 而不是 std::vector?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Marko Smith

    我正在尝试仅使用海龟随机和数学模块来制作吃豆人游戏

    • 1 个回答
  • Martin Hope
    Aleksandr Dubinsky 为什么 InetAddress 上的 switch 模式匹配会失败,并出现“未涵盖所有可能的输入值”? 2024-12-23 06:56:21 +0800 CST
  • Martin Hope
    Phillip Borge 为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行? 2024-12-12 20:46:46 +0800 CST
  • Martin Hope
    Oodini 具有指定基础类型但没有枚举器的“枚举类”的用途是什么? 2024-12-12 06:27:11 +0800 CST
  • Martin Hope
    sleeptightAnsiC `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它? 2024-11-09 07:18:53 +0800 CST
  • Martin Hope
    The Mad Gamer 何时应使用 std::inplace_vector 而不是 std::vector? 2024-10-29 23:01:00 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST
  • Martin Hope
    MarkB 为什么 GCC 生成有条件执行 SIMD 实现的代码? 2024-02-17 06:17:14 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve