When the code duplication is the right thing to do?

It’s a line that I’ve heard several times from different sources when learnt how to program: “If you write the same line more than once, move it to a function.” As a rule of thumb it seems OK, but definitely has some exceptions, where it’s better to do another way.

What’s your experience on when the code duplication is good?