@missmyluv The only correct answer to this question is "it depends". There are several rent vs buy calculators that people have linked to which will help you figure it out.
I will say though that for the length of time you mention (30-40 years), it is always going to be better to buy because...