In a way it's not so different from learning in school. There were definitely times I read a textbook for a programming or other CS course and reused some example code or whatever to solve homework problems.
I think the helpful part is taking something that already exists, learning how it works, and rewriting it yourself to internalize it.
Again as usual the more important thing is that you know what you're using otherwise it's just copying.