1
我是Rust新手,我試圖編寫hostname
實用程序來構建core-utils的生鏽backport。這裏更多:https://github.com/uutils/coreutilsRust,如何與gethostname()相互作用std :: libc
我有以下程序:
use std::libc;
extern {
pub fn gethostname(name: *libc::c_char, size: libc::size_t) -> libc::c_int;
}
fn main() {
unsafe {
let len = 34 as uint;
let mut buf = std::vec::with_capacity(len);
std::vec::raw::set_len (&mut buf, len as uint);
gethostname (std::vec::raw::to_ptr(buf), len as u64);
println(format!("{:?}", buf));
println(format!("{:?}", len));
//println(std::str::from_chars(buf));
}
}
我試圖打印到任何的gethostname份焦炭中的載體,但我得到的東西看起來並不像一個字符串。
~[65i8, 108i8, 97i8, 110i8, 115i8, 45i8, 77i8, 97i8, 99i8, 66i8, 111i8, 111i8, 107i8, 45i8, 80i8, 114i8, 111i8, 46i8, 108i8, 111i8, 99i8, 97i8, 108i8, 0i8, 0i8, 0i8, 0i8, 0i8, 0i8, 0i8, 0i8, 0i8, 0i8, 0i8]
34u
我需要爲了做到: 1.確保的gethostname()是做什麼的,我認爲是幹什麼的? 2.確保我編碼正確嗎?