【问题标题】:If the same string literal appears in code twice, does it appear in the executable once?如果相同的字符串文字在代码中出现两次,它是否会在可执行文件中出现一次?
【发布时间】:2022-11-10 12:40:06
【问题描述】:

假设我创建了一些结合了许多重复字符串文字的 Rust 源代码。它们是否在编译过程中进行了重复数据删除?

【问题讨论】:

    标签: rust


    【解决方案1】:

    是的!如果您创建以下程序,它打印两个变量的内存地址,您将看到它们打印相同的值。也就是说,ab 都引用相同的基础数据。

    fn main() {
        let a = "Hello";
        let b = "Hello";
        
        println!("{:p} {:p}", a, b);
    }
    

    要亲自尝试,您可以run the program within the Rust playground

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-03
      • 2016-03-19
      相关资源
      最近更新 更多