The way it works, you can call TargetUnit("Rare") in your script anytime you want.
If "Rare" doesn't exist in your vicinity - nothing will happen.
However, if "Rare" does exist then this call will fail because you are not allowed to target units w/o hardware event and you will get a standard error frame popping up telling you that.
And that's how you detect a rare - by causing you script to fail.
To make it more practical you might want to add a reload UI button and add a check if your target is alive so not to trigger that fail again over the corpse but strictly it terms of detection it's the fastest and highest range way to do it.
I used this addon for a couple of hours and I couldn't find any out of 8 rares I was chasing, could not believe they are not there. Unfortunately, the nameplate detection has very limited range and requires you to fly very low and slow, your field of vision is so small you miss everything.
As a solution to this, I wrote a tiny addon that spams a bunch of TargetUnit() commands a few times per second and believe it or not I found all 8 rares over the same areas where I failed before.
The idea is that you can not programmably target units, so when the unit is actually present the execution of TargetUnit() will produce an error on your screen and that's how your detect it. The range in this case is huge comparing to the nameplates so it works much much better.
I'm studying WoW add-on programming, can you please tell me, in general, how the minimap detection works? As far as I can see a minimap icon for a rare is not a part of the UI framestack. How do you detect it? Thank you!