Abdullah bin Masud reported,

We were with the Prophet ﷺ when he said, "Whoever among you is able to marry should do so, for it lowers the gaze and guards the private parts. Whoever is unable to marry should fast, for fasting is a shield for him."

Azami 4401

Sahih

عن عبد الله بن مسعود، قال

كنا مع النبي ﷺ فقال: من استطاع الباءة فليتزوج، فإنه أغض للبصر، وأحصن للفرج، ومن لم يستطع فعليه بالصوم فإنه له وجاء.

الأعظمي 4401صحيح
Contents
Reference
Commentary
Ayat
Collection
Jami al-Kamil
Book
Fasting
Chapter
Voluntary fasting and encouragement to do it
Section
Fasting was prescribed for one who cannot afford marriage and fears for himself
Numbering
Jami al-Kamil 4401 (2nd edition)
Azami 4401