main
use sha2::{Digest, Sha256}; mod hello; #[derive(Debug)] struct Name { family: String, first: String, age: i32, } fn main() { let n = hello::FamilyName { family: "tanaka".to_string() }; hello::print_hello(); println!("{:?}", n); let hash = Sha256::digest("hello world"); println!("{:x}", hash); }
sub
use sha2::{Digest, Sha256}; use crate::Name; #[derive(Debug)] pub struct FamilyName { pub family: String, } pub fn print_hello() { let n = Name {family: "yamada".to_string(), first: "taro".to_string(), age:15}; let hash = Sha256::digest("hello world"); println!("hello world! {:?}", n); println!("{:x}", hash); }
hello world! Name { family: “yamada”, first: “taro”, age: 15 }
b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
FamilyName { family: “tanaka” }
b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
ライブラリの呼び出しは、メイン、サブ両方のヘッダで書くことができる
メインがsubを呼び出す際は、mod hello;、サブがメインを呼び出す際は、use crate::Name;で呼び出せる。
クライアント側
fn main() { let coninfo = args[1].to_string() + ":" + &args[2]; let coninfo = TcpListener::bind(coninfo).expect("COuld not bind"); let mut stream = TcpStream::connect(coninfo).expect("could not connect to Server"); loop { let mut input = String::new(); let mut buffer: Vec<u8> = Vec::new(); io::stdin().read_line(&mut input).expect("Failed to read from stdin"); stream.write(input.as_bytes()).expect("Failed to write to server"); let mut reader = BufReader::new(&stream); reader.read_until(b'\n', &mut buffer).expect("Could not read intop buffer"); print!("{}", std::from_utf8(&buffer).expect("Could not write buffer as string")); } }