What about GitHub Codespaces? But it's more like a browser-based, full featured IDE and looks a bit too heavy. I am not sure how much it is long-term "free" and "durable" option.
We should always place the core part of the solution here at SO, so if the external link becomes broken, the answer would be still usable.
So any additional code example should be treated as an optional addition.
Benefits of Codespaces (and similar external services):
- runnable tests
- better source code highlighting
- multiple files support
- show file structure
- show imports of eventual dependent libraries
I think Stack Overflow should partner with a reputable external provider. Like they did with Imgur in regard to referenced images.
Partner with someone reputable to guarantee that the source examples won't go offline or behind a paywall; to ensure that the sources would be accessible at least in the read-only mode even if the external provider goes out of business.