How to mlock the memory allocated by a Vec?

I got the following code: Rust Playground

It is meant to simply allocate memory for the sake of machine testing.
However, if a system has got swap, the allocated pages will slowly be paged out to swap.

A solution (in C) is to mlock() the pages.
How can I run mlock() to lock the Vec 'hog' in memory?

On Linux/UNIX, you can use the libc crate: mlock in libc - Rust.

I modified your playground code with the following changes. It seems to be working:

use libc::{mlock, c_void};
// ...
    let ptr = hog.as_ptr() as *const c_void;
    let ret = unsafe { mlock(ptr, amount_as_u8) };
    assert_eq!(ret, 0);
    hog[0..amount_as_u8].fill(1);
    assert_eq!(hog[amount_as_u8 - 8..], [1u8; 8]);