RuP nanoparticles (NPs) encapsulated in uniform N,P-codoped hollow carbon nanospheres (RuP @NPC) have been synthesized through a facile route in which aniline-pyrrole copolymer nanospheres are used to disperse Ru ions followed by a gas phosphorization process. The as-prepared RuP @NPC exhibits a uniform core-shell hollow nanospherical structure with RuP NPs as the core and N,P-codoped carbon (NPC) as the shell. This strategy integrates many advantages of hollow nanostructures, which provide a conductive substrate and the doping of a nonmetal element. At high temperatures, the obtained thin NPC shell can not only protect the highly active phase of RuP NPs from aggregation and corrosion in the electrolyte but also allows variation in the electronic structures to improve the charge-transfer rate greatly by N,P codoping. The optimized RuP @NPC sample at 900 °C exhibits a Pt-like performance for the hydrogen evolution reaction (HER) and long-term durability in acidic, alkaline, and neutral solutions. The reaction requires a small overpotential of only 51, 74, and 110 mV at 10 mA cm in 0.5 m H SO , 1.0 m KOH, and 1.0 m phosphate-buffered saline, respectively. This work provides a new way to design unique phosphide-doped carbon heterostructures through an inorganic-organic hybrid method as excellent electrocatalysts for HER.