lotsoftools

Understanding Rust Error E0755

Introduction to Rust Error E0755

Rust Error E0755 occurs when the ffi_pure attribute is incorrectly applied to a non-foreign function. This attribute is specifically designed for foreign functions that do not have side effects or infinite loops.

Erroneous Code Example

#![feature(ffi_pure)]

#[ffi_pure] // error!
pub fn foo() {}

fn main() {}

In the above code example, the ffi_pure attribute is used on the non-foreign function 'foo', leading to Rust Error E0755.

Correct Usage of ffi_pure Attribute

To properly use the ffi_pure attribute, it must be applied only to foreign functions with no side effects or infinite loops, as shown in the example below:

#![feature(ffi_pure)]

extern "C" {
    #[ffi_pure] // ok!
    pub fn strlen(s: *const i8) -> isize;
}

fn main() {}

Here, the ffi_pure attribute is correctly applied to the foreign function 'strlen'. This usage will not trigger Rust Error E0755.

Further Information on Rust Error E0755

You can find more information about ffi_pure attribute and its correct usage in the unstable Rust Book. Learning and understanding the ffi_pure attribute can help prevent Rust Error E0755 in your Rust projects.