Hacker News new | ask | show | jobs
by voorwerpjes 1359 days ago
Very cool!

I don't understand the motivation about lifetimes in sync rust not being able to be arbitrary. I'm also confused because most of the time went you want to send data around in a async context you wrap it in `arc`, which has the pretty much analogous `rc` in a sync context which would also solve the lifetimes issue. Is there something I am missing?

1 comments

In C++, this will compile:

    class T {
        public:
            T(int & m): member{m} {}
            int & member;
    }

    T MakeT(int m) {
        return T(m);
    }
    
    int main() {
        const auto t = MakeT(10);
        std::cout << t.member << std::endl; // UB <- member has actually been destroyed
    }
Rust will correctly observe that the lifetime of m in `MakeT` is lower than the T object returned and will refuse to compile