For some women, enrolling in an engineering course is like running a psychological gauntlet. If they dodge overt problems like sexual harassment, sexist jokes, or poor treatment from professors, they often still have to evade subtler obstacles like the implicit tendency to see engineering as a male discipline. It’s no wonder women in the U.S. hold just 13 to 22 percent of the doctorates in engineering, compared to an already-low 33 percent in the sciences as a whole.

Nilanjana Dasgupta, from the University of Massachusetts in Amherst, thinks that mentors—people who can give advice, share experiences, or make social connections—can dismantle the gauntlet, and help young women to find their place in an often hostile field.

In a year-long study—one of the strongest yet to look at the value of mentorship—Dasgupta showed that female engineering undergraduates who are paired with a female mentor felt more motivated, more self-assured, and less anxious than those who had either no mentor or a male one. They were less likely to drop out of their courses, and keener to look for engineering jobs after they graduated. “Often, science is messy and things don’t turn out neatly,” Dasgupta says. But in this study, “it was very gratifying how clean the results were.”

She sees mentors as “social vaccines.” Just as medical vaccines prepare the immune system to deal with infections, good mentors inoculate the mind against the stultifying effects of negative stereotypes. “And this study isn’t just about women,” adds Radhika Nagpal, from Harvard University. “It’s about all the groups who have been historically and legally excluded, and are now slowly entering a world from which their members were barred. There’s a famous saying: You can’t be what you can’t see.”

Between 2011 and 2015, Dasgupta and her colleague Tara Dennehy recruited 150 women who enrolled in the university’s engineering course, and randomly assigned them to either a female mentor, a male mentor, or no mentor. The mentors were all high-performing senior students who shared the same majors as their mentees. After a brief training session, they were asked to meet with their charges once a month, and help them to wrestle with academic problems, develop long-term plans, find a social network, and more.

A year later, Dennehy and Dasgupta surveyed the volunteers. Compared to their mentor-less peers, the students with female mentors felt more accepted by their peers and less invisible. They were more confident in their engineering skills, and more likely to think they had a talent for the subject. They were more likely to think that their ability to overcome their academic challenges outweighed the stress and uncertainty they felt.

“It’s not that having a female mentor increased belonging or confidence—it just preserved it,” Dasgupta notes. This is a critical point. Without any mentorship at all, the volunteers felt increasingly anxious, under-confident, and out of the place through the year. But the mentors helped to straighten these arcs that, by default, veer towards exclusion and attrition.

That has long-term effects. As the months wore on, Dennehy and Dasgupta found that women without mentors increasingly thought about switching majors, and became less keen on pursuing graduate degrees in engineering. By the end of the first year, 11 percent of them had dropped out. By contrast, the students with female mentors remained equally committed to their fields, and every single one of them stayed the course.

Why? The answer had nothing to do with academic performance: The students’ actual grades had no bearing on their odds of staying in engineering. Instead, “the active ingredients are belonging and confidence,” says Dasgupta. “Humans are social animals. Our ability alone doesn’t determine whether we stay in or leave a field. It’s ability mixed in that feeling that these are your people, this is where you belong. Absent, that even high-performers might not feel motivated to stay.” Which makes you wonder: How many brilliant minds have been lost from engineering and other STEM disciplines because those disciplines didn’t create spaces for them?

Dennehy and Dasgupta also found that male mentors were somewhat of a mixed bag. In some measures, they were just as effective as female mentors. In others, they were indistinguishable from having no mentor at all. And in some cases, they were worse: They actually increased women’s anxiety about their performance over time.

Why? Dasgupta expected that the female mentors would provide more social and emotional support—but that wasn’t the case. The mentors all kept diaries about their conversations with their mentees, and these revealed that both genders largely talked about the same kinds of academic problems. And the mentees themselves felt that the male mentors were just as supportive and available as the female ones. Instead, Dasgupta speculates that the men just couldn’t act as role models in the same way that other women could, and so couldn’t catalyze those all-important feelings of belonging.

That’s not to say that the men have no role to play. “They could connect women to other women in engineering, or to female faculty who could do the work of social belonging in a way that the male mentors can’t provide themselves,” Dasgupta says.

Lin Bian from the University of Illinois, who recently showed that gendered stereotypes about intelligence take root at the young age of 6, says that Dasgupta’s study reveals how “role models inoculate women against negative beliefs during critical transitions.” The start of college is one such transition—a point when life gets upended, and when people feel a surge of uncertainty about their place in the world. That’s when mentoring can make the most difference.

“It makes an incredible case for near-peer mentoring to increase the graduation rate of women in engineering degree programs,” says Sheila Boyington, the president of the Million Women Mentors initiative. “While much of the study is indeed intuitive, it affirms a well-defined path forward for universities to follow if they want to increase diversity in STEM.”

And although mentoring is just one of many possible solutions, “it’s not an either-or situation,” says Nagpal. “We need to do everything, like mentoring, fighting sexist exclusionary behaviors, training men to behave better, and investing money in better practices. We need to make up for a century of neither-nor.”