Читаем Rust by Example полностью

let child = thread::spawn(move || {

// The thread takes ownership over `thread_tx`

// Each thread queues a message in the channel

thread_tx.send(id).unwrap();

// Sending is a non-blocking operation, the thread will continue

// immediately after sending its message

println!("thread {} finished", id);

});

children.push(child);

}

// Here, all the messages are collected

let mut ids = Vec::with_capacity(NTHREADS as usize);

for _ in 0..NTHREADS {

// The `recv` method picks a message from the channel

// `recv` will block the current thread if there are no messages available

ids.push(rx.recv());

}

// Wait for the threads to complete any remaining work

for child in children {

child.join().expect("oops! the child thread panicked");

}

// Show the order in which the messages were sent

println!("{:?}", ids);

}

הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

<p id="path"><strong><a l:href="#path">Path</a></strong></p>

The Path struct represents file paths in the underlying filesystem. There are two flavors of Path: posix::Path, for UNIX-like systems, and windows::Path, for Windows. The prelude exports the appropriate platform-specific Path variant.

A Path can be created from an OsStr, and provides several methods to get information from the file/directory the path points to.

Note that a Path is not internally represented as an UTF-8 string, but instead is stored as a vector of bytes (Vec). Therefore, converting a Path to a &str is not free and may fail (an Option is returned).

use std::path::Path;

fn main() {

// Create a `Path` from an `&'static str`

let path = Path::new(".");

// The `display` method returns a `Show`able structure

let _display = path.display();

// `join` merges a path with a byte container using the OS specific

// separator, and returns the new path

let new_path = path.join("a").join("b");

// Convert the path into a string slice

match new_path.to_str() {

None => panic!("new path is not a valid UTF-8 sequence"),

Some(s) => println!("new path is {}", s),

}

}

הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה

Перейти на страницу:

Похожие книги