Depends on the doctrine of your legal system though. The USA have a protection doctrine in their law, in Germany they want to install a similar "nonsense" (my opinion on it), but currently the prime driving force behind laws here in our doctrine is managing the freedom of people. Raping a person is limiting this persons freedom and thus is illegal. Simple. You are not punished for protecting the country from you (we have special legal cases for protecting the society, getting used when absolutely no chance is seen that the legal punishment will change your behavior), you are punished because you exceeded your personal freedom at the expense of others.
There's not really a difference that I recognize. I'm not sure exactly what you mean by "managing" freedom, but securing citizens ("safety" is perhaps not a satisfactorily descriptive choice of words), includes securing their freedom - which means that people are free to do what they want, except to the extent that what they want, is to do harm to others (including abrogating the freedom of others; "kidnapping" is such a crime, and there are other, more specifically applied, ones).
The words that U.S. tradition would tend to use, are those of our Declaration of Independence: government's natural purpose is to "secure unalienable Rights." In practice, this means that government's purpose is to enable the persistence of a state of civilization, by maintaining/securing the necessary condition for civilization, which is that the participants don't violate each other (and neither does anyone else; e.g. - a foreign invader) - they don't, by force, impose their own will upon their neighbor (in recognition of, and deference to, their neighbor's human nature to act according to his own will); they don't do physical harm to their neighbor, and they don't constrain his freedom to pursue his happiness as he conceptualizes it. That is a necessary circumstance, in order for people to be willing to hang out with each other, to associate with one another, and for a persistent state of civilization to be possible. Human nature is respected, individual freedom is not constrained, to the extent that
everybody is free - which means that they are not free to violate each other. Government secures this condition, by
making sure that they don't violate each other - defeating, by superior force, force that is directed against an individual (as a practical matter, such intervention tends usually to be after-the-fact, intervening to prevent a
recurrence of such misbehavior). Thus, government's natural purpose, and value - and the ethical justification for its being an agency of violence - is that it is citizens' common agent of their self-defense, thereby enabling a persistent state of civilization.
Government's function is to use force to compel behavior. Obviously, this is a constraint upon individual freedom. The principal, civil value and utility of such an agency, such power, is to compel behavior insofar as it is necessary to compel people not to harm one another, so that they are able to maintain the persistent association that characterizes a state of civilization. I suppose that one can describe that as "managing freedom," but that's an unpleasantly open-ended description; government's acts of constraining freedom, are desirably regarded as quite limited to what is necessary for common defense; otherwise, its violence is really no different from criminal violence, being simply the use of force to subjugate others to one's own will - ethically justifiable, insofar as one's will is that he not be violated, but otherwise merely arrogance (and likely to be incompatible with a persistent state of civilization).
It is not about protecting the society - how could a judge protect the society from rapes that way, when a illegal and unpleasant thought police would be needed? And does raping a girl really remove the criminal from the society or does the criminal also have rights, which need to be protected?
Well, that's a difficult, practical problem, of course. The general principle (in the USA) is that such a "threat to society" is identified by a specific, overt act - a person's act of having actually committed a crime, with that having been publicly proven beyond reason for doubt. And the alleged criminal has a Right to be regarded as
not a threat to society - to be "presumed innocent" -
unless his having committed a crime has, indeed, been so proven. And he has a Right, that the accusation that he committed a crime, is treated according to the procedural standards intended to ensure that a conviction (his being judged guilty of the crime) exemplifies a reliable supposition that it is factually
true that he committed the crime. And he furthermore has a Right, that the consequences of a conviction do not include brutality or other treatment in excess of what is necessary to constrain him from doing further harm to others.
If you rape a girl, you can get "only" a serious punishment here, because it is usually assumed (possibly correctly), that such behavior can be corrected by psychological treatment during and after your stay in prison. Only when even the most optimistic stockholm-syndromed psychologist waves off, you are considered a permanent threat for the society and requiring "special attention". This then means your freedom of movement will be permanently limited for the rest of your life(Called "Sicherungsverwahrung" - in English literally "protective storage"). You can still get free in some cases afterwards, but this then requires a lot of stockholm-syndromed psychologists to believe that you are no longer a threat for society. Has happened in the past, resulted in new crimes, made many people angry, and thus, the requirements for being no longer a threat had been raised. For preventing that psychologists in such cases become considered a threat for the society and might become directly liable for their decisions...
This is probably the most difficult problem of criminal justice: what is an appropriate remedy, once the threat is identified; what "punishment," or other treatment, will secure the society against the threat, while also being no more than is necessary to that purpose (and even acknowledging the possibility that a criminal conviction may be erroneous).
I have described only the conceptual framework, as I understand it, and in the context of the issues being discussed; the details of how to accomplish the various conceptualized purposes, are a continuing practical difficulty, and they are always being considered and debated.