That’s terrible. If you’re unlucky enough to get caught up in a rip, always swim parallel to the beach until you get yourself out of the current, and failing that, focus your energy on staying floating.
Ideally you notice it from the shore and avoid it as others have mentioned. If you're swimming in one you'll realize soon enough you're getting taken out from the shore.
I got caught in one when I was a teenager off Mission Beach in San Diego. I'd already been out swimming in water deeper than I could stand in for a while and, getting tired, started heading to shore. After some time I realized I wasn't making any progress at all. It took me a little while more to realize what was up: I was in a rip current. Thankfully I'd had an elementary school teacher in Phoenix, AZ of all places that taught us kids how to escape one and I remembered - swim parallel to the shore a good distance and then try swimming back in again and check your progress. Repeat as needed if you're unable to make progress. I followed those instructions and eventually was able to get back to shore, utterly exhausted. I can't help but think how lucky I was not to have missed that day of school.
If you've ever played in the surf and can feel when a wave pulls you towards the ocean before another wave pushes you back towards the beach. It's like that, but just the undertow part. It's very noticable.
From the shore you can actually see them. It’s an area with fewer waves than the surrounding surf; the water can appear either deeper in color or sandy/seaweedy. The photos online will show you better than I can describe. If you get caught by one while in the ocean, you’ll know. The current will be strongly pulling you away from the shore.