Hello,

I am taking some time to experiment with Rust and try to get a better understanding of the syntax and semantics. I have been experimenting with small programs and just wanted to check and see if I am understanding things correctly.

* If you have a variable of type "T" (for example, "int" or "vec[int]"), this is a value type. That is, the value itself is generally immutable, with the exception of objects and records, which may have mutable fields. This is essentially the same as in O'Caml.

* A box type like @T is an instance of type T that lives in the heap. The value in the box is immutable. To make it mutable, you do @mutable T.

* An alias type like &T is a pointer to a value of type T. This value must live either on the stack or in the field of a record. It may point to a mutable location or a non-mutable location.

* A mutable alias type like "& mutable T" is a pointer to a mutable location of type T.

* Equality is "deep" equality for values of type "T", "&T", "*T", pointer equality for mutable values like "@mutable T" or records with mutable fields.

In particular, I am trying to understand the precise intention beyond alias types (&T). Is it correct that an alias type points to some variable whose address is guaranteed to outlive the current function? The compiler presumably wants to be able to copy values of type &T without worrying about reference counts or garbage collection? This would explain why a variable "x" of type "@T" cannot be used as the value for a parameter of type "&T", and one must write "*x" instead, because "*x" makes a temporary copy of the value at the time of the call.

I haven't begun looking at unique types like "~T" yet, but I know they exist. Not sure if there are other type variations I am not aware of.

I haven't explored memory management much in my own tests, but from reading threads etc


Niko
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to