It is a shame, but we have to be careful due to these kind of events.
I always try to mindful of surroundings, never allow self to be distracted by phone or music and keep distance. I also have taken self defense classes. The expectation isn't to beat the attacker as men naturally can overpower us, but to either escape or deter. Pepper spray or a rape whistle are good suggestions.
Sadly, even with every precaution things can go badly quickly, and others I have spoken with take these courses due to attacks similar to yours. At the very least it can be a method to work through trauma and regain confidence in yourself. Best of luck to you Onionberry.