1

Normally, there is only one owner for a specific value (except for things like Rc<T>). Then what is the owner of the value 4 below since the variable myVar borrows it from something? I want to know what is that something.

let myVar = &4;
Wesley Wiser
  • 9,491
  • 4
  • 50
  • 69
주피터
  • 135
  • 6
  • 3
    It does not belong to anything. `&4` is just a reference to something. In this case a reference to some memory location where a 4 is. – hellow Aug 06 '19 at 10:59
  • 4
    It is static. There is no owner because it is in static memory so exists for the duration of the application. – Peter Hall Aug 06 '19 at 11:01
  • 3
    Alternatively, you could think of it as being 'owned' by the program itself. – Joe Clay Aug 06 '19 at 11:07
  • all above make senses, and being 'owned' by the program itself makes me clear. Thanks all. – 주피터 Aug 07 '19 at 00:02

1 Answers1

6

Literals, be they:

  • number literals, like 4
  • string literals, like "Hello, World"

Have a 'static lifetime as their value is hard-coded into the library or executable itself. For example, on Linux, they would be found either in the .text segment or the .rodata segment of the ELF binary.

In that sense, you can think of them as being owned by the program itself.

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722