The Prophet ﷺ said, "Whoever among you is able to marry should do so, for it lowers the gaze and guards the private parts. Whoever is unable should fast, for fasting is a shield for him."
أخبرنا بشر بن خالد قال حدثنا محمد بن جعفر عن شعبة عن سليمان عن إبراهيم عن علقمة أن ابن مسعود لقي عثمان بعرفات فخلا به فحدثه وأن عثمان قال لابن مسعود هل لك في فتاة أزوجكها فدعا
أن النبي ﷺ قال من استطاع منكم الباءة فليتزوج فإنه أغض للبصر وأحصن للفرج ومن لم يستطع فليصم فإن الصوم له وجاء
Sourced from Bukhari, Sawm, chapter on fasting for one who fears celibacy will harm him, hadith no. 1905, and Muslim, hadith no. 1400/1, see the previous hadith narrated from Sulayman Amash, and it is also in Kubra, hadith no. 2548.