Real life example of unsafe code usage

Hi All,

I'm still trying to wrap my head around a real-life use case for unsafe code. Does anybody have an example (or repo) of some being used?

Many thanks,

The stdlib contains lots of them. For example, the Vec<T> is implemented with unsafe code.

1 Like

Thank you for this, I forgot to mention that I am aware that they are used in the stdlib. I was wondering what someone would use it for in a project example. I do appreciate the existence of unsafe code, but I was wondering where it'd be required in a project.

The Redox operating system kernel also contains handful of unsafe code.

1 Like

Thank you, will have a look through this!

I addressed this in a recent related thread. FFI and interfacing to a GPU are two obvious uses that are currently unavoidable.

1 Like

In general "unsafe" is not required in application code.

My plan is never to write "unsafe" into my applications. After all I don't in my Javascript/node.js or Python programs, why should I do it in Rust?

Well, actually we do use "unsafe" code in node.js and Python, not to mention C#, Java etc. We do it by writing modules in C or C++ that we can call from our application language.

Many times people have suggested use of "unsafe" to get C like performance from some Rust code I have presented here. Every time it has proved unnecessary, one could get the performance without "unsafe".

1 Like

Thank you @ZiCog for taking the time to explan, very much appreciated!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.