The Messenger of Allah ﷺ said to us, "Young men, whoever among you can afford marriage should marry, for it lowers the gaze and guards the private parts. And whoever cannot should fast, for fasting is a shield for him."
قال لنا رسول الله ﷺ: يا معشر الشباب من استطاع منكم الباءة فليتزوج، فإنه أغض للبصر، وأحصن للفرج، ومن لم يستطع فعليه بالصوم، فإنه له وجاء.
Bukhari narrated it in the Book of Marriage (5065), and Muslim in the Book of Marriage (1400:1), both of them through the route of Amash, who said, Ibrahim (that is, Nakhai) told me, from Alqama, from Abdullah bin Masud, who said, and he mentioned it. And His saying, "And do not compel your slave girls to prostitution if they desire chastity" [Nur 24:33]