As Hamlet is contemplating suicide, he equates sleep with death.
But then he imagines what death is like, and worries that if there
are dreams or something after, it would be bad:
"To die, to sleep -
To sleep, perchance to dream - ay, there's the rub,
For in this sleep of death what dreams may come..."