I haven't used Rest on a Pokemon since like Generation II so the mechanics of the game may have changed, but from what I remember, even if your Pokemon switches out, if the battle ends, etc. the counter for Rest usually remains. However, I am playing Pokemon Y right now, and had my Ferrothorn use rest on a wild Pokemon, waited a turn, and then ran. On the next encounter of a Pokemon, it was asleep for another two full turns before running, and then on the last encounter of a Pokemon, it didn't wake up until the third turn. Why is this?