rust

Why can immutable variables be passed as arguments to functions that require mutable arguments?

雨燕双飞 提交于 2020-05-15 06:19:08
问题 Example code: fn main() { let a = [1, 2, 3, 4, 5]; reset(a); } fn reset(mut b: [u32; 5]) { b[0] = 5; } The variable a is an immutable array, and the reset function's parameter b is a mutable array; intuitively I need to modify a to a mutable array before I can call the reset method, but the compiler tells me that I don't need to do this, why is this? fn main() { let mut a = [1, 2, 3, 4, 5]; reset(a); } fn reset(mut b: [u32; 5]) { b[0] = 5; } warning: variable does not need to be mutable -->

Why does a HTTP GET request with vanilla Rust get no answer?

与世无争的帅哥 提交于 2020-05-15 05:25:30
问题 I have the following code but when I start the app it requests the page but gets no answer. use std::io::Read; use std::io::Result; use std::io::Write; use std::net::TcpStream; fn main() { if let Err(err) = connect() { println!("err = {}", err); } } fn connect() -> Result<()> { let mut stream = TcpStream::connect("www.google.de:80")?; let mut request_data = String::new(); request_data.push_str("GET / HTTP/1.1"); request_data.push_str("\r\n"); request_data.push_str("Host: www.google.de");

How do I use the Option::ok_or() method correctly?

微笑、不失礼 提交于 2020-05-15 05:12:28
问题 I'm trying to understand how to use the question mark operator for error handling in Rust. I have this code: fn main() -> Result<(), &'static str> { let foo: i32 = Some("1") .ok_or(Err("error 1"))? .parse() .or(Err("error 2"))?; Ok(()) } This code can not be compiled for some reason: error[E0277]: the trait bound `&str: std::convert::From<std::result::Result<_, &str>>` is not satisfied --> src/main.rs:2:20 | 2 | let foo: i32 = Some("1") | ____________________^ 3 | | .ok_or(Err("error 1"))? |

How do I use the Option::ok_or() method correctly?

…衆ロ難τιáo~ 提交于 2020-05-15 05:11:42
问题 I'm trying to understand how to use the question mark operator for error handling in Rust. I have this code: fn main() -> Result<(), &'static str> { let foo: i32 = Some("1") .ok_or(Err("error 1"))? .parse() .or(Err("error 2"))?; Ok(()) } This code can not be compiled for some reason: error[E0277]: the trait bound `&str: std::convert::From<std::result::Result<_, &str>>` is not satisfied --> src/main.rs:2:20 | 2 | let foo: i32 = Some("1") | ____________________^ 3 | | .ok_or(Err("error 1"))? |

What does “Stream did not contain valid UTF-8” mean?

一个人想着一个人 提交于 2020-05-15 03:46:21
问题 I'm creating a simple HTTP server. I need to read the requested image and send it to browser. I'm using this code: fn read_file(mut file_name: String) -> String { file_name = file_name.replace("/", ""); if file_name.is_empty() { file_name = String::from("index.html"); } let path = Path::new(&file_name); if !path.exists() { return String::from("Not Found!"); } let mut file_content = String::new(); let mut file = File::open(&file_name).expect("Unable to open file"); let res = match file.read_to

What does “Stream did not contain valid UTF-8” mean?

馋奶兔 提交于 2020-05-15 03:45:07
问题 I'm creating a simple HTTP server. I need to read the requested image and send it to browser. I'm using this code: fn read_file(mut file_name: String) -> String { file_name = file_name.replace("/", ""); if file_name.is_empty() { file_name = String::from("index.html"); } let path = Path::new(&file_name); if !path.exists() { return String::from("Not Found!"); } let mut file_content = String::new(); let mut file = File::open(&file_name).expect("Unable to open file"); let res = match file.read_to

How to lend a Rust object to C code for an arbitrary lifetime?

一个人想着一个人 提交于 2020-05-14 20:14:32
问题 I'm writing a library in Rust that has a C interface. C side must be able to create and destroy Rust objects (C side owns them and controls their lifetime). I've managed to "leak" an object to C, but I'm not sure how to properly free it: pub extern "C" fn create() -> *mut Foo { let obj = Foo; // oops, a bug let ptr = std::mem::transmute(&mut obj); // bad std::mem::forget(obj); // not needed return ptr; } pub extern "C" fn destroy(handle: *mut Foo) { // get Foo back and Drop it??? } I'm not

How to lend a Rust object to C code for an arbitrary lifetime?

元气小坏坏 提交于 2020-05-14 20:08:10
问题 I'm writing a library in Rust that has a C interface. C side must be able to create and destroy Rust objects (C side owns them and controls their lifetime). I've managed to "leak" an object to C, but I'm not sure how to properly free it: pub extern "C" fn create() -> *mut Foo { let obj = Foo; // oops, a bug let ptr = std::mem::transmute(&mut obj); // bad std::mem::forget(obj); // not needed return ptr; } pub extern "C" fn destroy(handle: *mut Foo) { // get Foo back and Drop it??? } I'm not

How to lend a Rust object to C code for an arbitrary lifetime?

≡放荡痞女 提交于 2020-05-14 20:07:33
问题 I'm writing a library in Rust that has a C interface. C side must be able to create and destroy Rust objects (C side owns them and controls their lifetime). I've managed to "leak" an object to C, but I'm not sure how to properly free it: pub extern "C" fn create() -> *mut Foo { let obj = Foo; // oops, a bug let ptr = std::mem::transmute(&mut obj); // bad std::mem::forget(obj); // not needed return ptr; } pub extern "C" fn destroy(handle: *mut Foo) { // get Foo back and Drop it??? } I'm not

Align struct to cache lines in Rust

廉价感情. 提交于 2020-05-14 20:00:51
问题 Assuming I wanted to (ab)use cache coherency to do lock free reads like FaRM, would it be enough to have a struct with a single 64 byte array as data to guarantee that on an architecture with 64 byte cache lines each struct would occupy exactly one cache line? 回答1: No, that wouldn't guarantee that the alignment was a cache line. RFC 1358 added the concept of #[repr(align)] , allowing the programmer to specify alignment requirements. This attribute was stabilized in Rust 1.25. For your