News for hackers. Stuff that matters.
Re:What Rust Can Do That Other Languages Can't by dorianm on Monday February 06, 2017 @04:28AM (#13577919)
Other languages can definitely do it:

Ruby:

    class X
      attr_accessor :y
    end
Crystal (full working example):

    class Y
    end

    class X
      def initialize(y : Y)
        @y = y
      end

      def y
        @y
      end
    end

    y = Y.new
    x = X.new(y: y)
    puts x.y
Re:What Rust Can Do That Other Languages Can't by willvarfar on Monday February 06, 2017 @04:30AM (#13577926)
The article is not about returning a reference to a struct member, its about returning a reference to something on the stack rather than heap.
Re:What Rust Can Do That Other Languages Can't by topspin on Monday February 06, 2017 @04:56AM (#13578056)
Rust ownership applies to heap allocated objects as well; if you return a reference to a member of a heap allocated struct that reference cannot outlive the struct allocation. The point of the article is more general than "stack vs heap;" other languages are either incapable of expressing a reference to a member of a heap allocated struct or incapable of ensuring the memory safety of such a reference.
Re:What Rust Can Do That Other Languages Can't by willvarfar on Monday February 06, 2017 @05:02AM (#13578081)
As the article notes, garbage-collected languages can ensure the memory safety of a pointer into a heap object.
Re:What Rust Can Do That Other Languages Can't by TuringTest on Monday February 06, 2017 @07:06AM (#13578684)
But not at compile time, which is what Rust provides.
Re:What Rust Can Do That Other Languages Can't by imron on Monday February 06, 2017 @04:37AM (#13577950)
And crucially, enforcing that no references to the returned member will outlive the containing struct.
Re:What Rust Can Do That Other Languages Can't by roca on Monday February 06, 2017 @06:27AM (#13578492)
I looked up Crystal. Crystal "class" objects are heap allocated, so in this example "y" is allocated as a separate heap object to the X. As I outlined in my post, that isn't what I was asking for.
Re:What Rust Can Do That Other Languages Can't by truantee on Monday February 06, 2017 @09:52AM (#13579889)
well Crystal "struct" objects are stack allocated.
Re:What Rust Can Do That Other Languages Can't by roca on Monday February 06, 2017 @05:02PM (#13584645)
If y was a struct, you couldn't return a reference to it AFAICT. Just like C# say.
Re:What Rust Can Do That Other Languages Can't by qznc on Monday February 06, 2017 @05:10AM (#13578129)
Any language where the X thing is managed by a garbage collector (and you cannot manually delete it anyways) is just as safe as Rust. What only Rust can do is to check your manually memory-managed stuff for correctness (conservatively).