Skip to content

docs: tutorial ffi: the example compilation fails #5871

Closed
@mdesantis

Description

@mdesantis

The compilation of the first example of Rust Foreign Function Interface Tutorial fails:

extern mod std;
use core::libc::c_uint;

extern mod crypto {
    fn SHA1(src: *u8, sz: c_uint, out: *u8) -> *u8;
}

fn as_hex(data: ~[u8]) -> ~str {
    let mut acc = ~"";
    for data.each |&byte| { acc += fmt!("%02x", byte as uint); }
    return acc;
}

fn sha1(data: ~str) -> ~str {
    unsafe {
        let bytes = str::to_bytes(data);
        let hash = crypto::SHA1(vec::raw::to_ptr(bytes),
                                vec::len(bytes) as c_uint,
                                ptr::null());
        return as_hex(vec::from_buf(hash, 20));
    }
}

fn main() {
    io::println(sha1(core::os::args()[1]));
}

with this error:

$ rustc main.rs && ./main asd
main.rs:25:21: 25:39 error: moving out of immutable vec content
main.rs:25     io::println(sha1(core::os::args()[1]));
                                ^~~~~~~~~~~~~~~~~~
error: aborting due to previous error

since the program argument, which is of ~str type, must be clone()'d:

fn main() {
    io::println(sha1(core::os::args()[1].clone()));
}
$ rustc main.rs && ./main asd
f10e2821bbbea527ea02200352313bc059445190

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-FFIArea: Foreign function interface (FFI)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions