All children go through hitting phases, usually it's just an expression of big feelings. In the example you gave, I would just ignore it until you've got him somewhere safe where you can describe what happened and the actions you took. At home, I just catch my son's hands (gently but firmly) and say, "I can't let you hit me." That's the advice given in this excellent book that's solved a lot of conflicts for us with our 4-year-old.
One thing I will say is don't try to reason with a 3-year-old, they can only really understand consequences after taking an action, they're far too impulsive to think about what they're doing. They might have a good enough memory to tell you what the consequence is if asked directly, but you have to catch them before the impulsive need to do takes over. I find it really hard to remember that sometimes because by 3 children start to have language skills that can make you forget how old they actually are.
Also, and obviously this is just for my situation, but - and I cannot emphasize this enough - this book is literal, serious, jedi fucking magic.
Pure mind-blown unbelievable how great it works.
It's literally like in the movies when they wave their hand & tell someone what to do / how to act - no BS.
https://www.amazon.com/Talk-When-Kids-Wont-Listen/dp/1982134143