This is one subject I wish I could avoid, but I have to be realistic here and face the fact our fuzzy friends do not live forever. Rats are great pets as you have come to know, but there is one thing wrong with them. They have a short life span. If any animal was ever "ripped off", it is the rat. Rats were robbed of a longer life span and if any animal deserves to live longer, it sure is the rat! The bond we create with them is very strong and we owe it to them to keep them out of pain and misery. After all options to get our rattie friend recover from an illness have been exhausted, we have one last gift to give them, the final gift of love, which is letting them go through euthanasia instead of letting them suffer until they die. When the time comes to say goodbye, the vet will be the one to put your rat to sleep. My advice to you is to stay with your rat the entire time to be sure that your rat is put down humanely. NEVER allow the vet to use the cardiac injection, even if the rat is sedated. This method is considered inhumane and very very painful. Always remember that. However, the cardiac injection can be used as long as the rat is under general anesthesia and I do mean DEEPLY under, as if they were about to have surgery performed. No other method is acceptable when it comes to a cardiac stick.
Once the rat has been given the injection that will help him fall asleep forever, stay with him, talking to him and petting him till the end. It would be your gentle voice and tender touch that is the last thing he or she sees. I have lost many beloved rats in the past and so this part is very hard for me to talk about, so I will take the cowards way out and send you off with another link that will discuss in greater detail the proper and humane way to let your rat go.
NEVER try to put your rat to sleep on your own. Too many people think they can do this using methods such as "dry Ice" where the chemical that dry ice emits is CO2. It is impossible to control the amount of CO2 that dry ice emits and therefore considered improper, irresponsible home euthanasia. In fact, NO at home method is safe nor humane to do. I am aware of several popular rat care websites that discuss ways to put your rat to sleep at home and some of these sites paint a picture of a peaceful and serene passing. Too many things can go wrong that can be absolutely devastating to the rat and its owner and rather than have a peaceful and painless passing, the rat may end up dying in horrific agony. The right thing to do is to go to a qualified Vet who will put your rat to sleep gently, painlessly and most of all, humanely!