Hacker News new | ask | show | jobs
by sakex 1359 days ago
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