gpt4 book ai didi

rust - static 或 const 中的 `&str` 和 `&' static str` 有什么区别?

转载 作者:行者123 更新时间:2023-11-29 07:47:44 25 4
gpt4 key购买 nike

我是 Rust 编程和学习生命周期的新手。

const CONST_MEEP: &str = "MEEP";
const CONST_LIFETIME_MEEP: &'static str = "MEEP";
static STATIC_MEEP: &'static str = "MEEP";
static STATIC_LIFETIME_MEEP: &str = "MEEP";

fn main() {
println!("CONST_MEEP is {}", CONST_MEEP);
println!("CONST_LIFETIME_MEEP is {}", CONST_LIFETIME_MEEP);
println!("STATIC_MEEP is {}", STATIC_MEEP);
println!("STATIC_LIFETIME_MEEP is {}", STATIC_LIFETIME_MEEP);
}

输出:

CONST_MEEP is MEEP
CONST_LIFETIME_MEEP is MEEP
STATIC_MEEP is MEEP
STATIC_LIFETIME_MEEP is MEEP

CONST_MEEPCONST_LIFETIME_MEEP 有什么区别? STATIC_MEEPSTATIC_LIFETIME_MEEP 有什么区别?

最佳答案

没什么,没有区别。截至RFC 1623 , staticconst 项中的引用自动为 'static。这在 Rust 1.17 生效.

关于rust - static 或 const 中的 `&str` 和 `&' static str` 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49684657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com