rust

Do values in return position always get allocated in the parents stack frame or receiving Box?

落花浮王杯 提交于 2020-05-13 01:49:56
问题 I was trying to understand how structs behave when returned from methods. There is a section in the nightlies section of the "Rust Book" that said if you used the syntax... let x = box i_return_a_struct(); .. that there wouldn't be a copy, therefore there is no need to return a pointer. But when I started playing with it, it appears that the box is not needed, unless you need the value to exist on the heap. #[derive(Debug)] struct Dummy { data: i64, } impl Drop for Dummy { fn drop(&mut self)

Do values in return position always get allocated in the parents stack frame or receiving Box?

白昼怎懂夜的黑 提交于 2020-05-13 01:48:48
问题 I was trying to understand how structs behave when returned from methods. There is a section in the nightlies section of the "Rust Book" that said if you used the syntax... let x = box i_return_a_struct(); .. that there wouldn't be a copy, therefore there is no need to return a pointer. But when I started playing with it, it appears that the box is not needed, unless you need the value to exist on the heap. #[derive(Debug)] struct Dummy { data: i64, } impl Drop for Dummy { fn drop(&mut self)

Rust/Diesel: How to query and insert into postgres tables which have uuid

心不动则不痛 提交于 2020-05-12 11:59:45
问题 I have the following schema generated by Diesel: table! { user (id) { id -> Uuid, name -> Text } and the associated model use diesel::{ self, Queryable, Insertable, }; use diesel::prelude::*; use diesel::sql_types::Uuid; use super::schema::user; #[derive(Queryable)] pub struct User { pub id: Uuid, pub name: String, } impl User { pub fn get(id: i32, connection: &PgConnection) -> Vec<User> { user::table.load::<User>(connection).unwrap() } } I get an error when I try to compile this which says:

Rust/Diesel: How to query and insert into postgres tables which have uuid

做~自己de王妃 提交于 2020-05-12 11:59:12
问题 I have the following schema generated by Diesel: table! { user (id) { id -> Uuid, name -> Text } and the associated model use diesel::{ self, Queryable, Insertable, }; use diesel::prelude::*; use diesel::sql_types::Uuid; use super::schema::user; #[derive(Queryable)] pub struct User { pub id: Uuid, pub name: String, } impl User { pub fn get(id: i32, connection: &PgConnection) -> Vec<User> { user::table.load::<User>(connection).unwrap() } } I get an error when I try to compile this which says:

Are operators in core really defined circularly?

别说谁变了你拦得住时间么 提交于 2020-05-12 11:27:40
问题 We can implement the traits in core::ops to define the behavior of operators for our types. The traits themselves are annotated with #[lang =...] attributes so the compiler knows which traits and operators belong together. For example, the Add implementation for primitive types looks like this (macro manually expanded and simplified from here): impl Add for i32 { type Output = i32; fn add(self, other: i32) -> i32 { self + other } } To my surprise, the implementation uses the + operator

How to match struct fields in Rust?

送分小仙女□ 提交于 2020-05-12 11:19:12
问题 Can Rust match struct fields? For example, this code: struct Point { x: bool, y: bool, } let point = Point { x: false, y: true }; match point { point.x => println!("x is true"), point.y => println!("y is true"), } Should result in: y is true 回答1: Can Rust match struct fields? It is described in the Rust book in the "Destructuring structs" chapter. match point { Point { x: true, .. } => println!("x is true"), Point { y: true, .. } => println!("y is true"), _ => println!("something else"), }

How to match struct fields in Rust?

两盒软妹~` 提交于 2020-05-12 11:18:20
问题 Can Rust match struct fields? For example, this code: struct Point { x: bool, y: bool, } let point = Point { x: false, y: true }; match point { point.x => println!("x is true"), point.y => println!("y is true"), } Should result in: y is true 回答1: Can Rust match struct fields? It is described in the Rust book in the "Destructuring structs" chapter. match point { Point { x: true, .. } => println!("x is true"), Point { y: true, .. } => println!("y is true"), _ => println!("something else"), }

How do I concatenate two slices in Rust?

╄→гoц情女王★ 提交于 2020-05-12 11:17:38
问题 I want to take the x first and last elements from a vector and concatenate them. I have the following code: fn main() { let v = (0u64 .. 10).collect::<Vec<_>>(); let l = v.len(); vec![v.iter().take(3), v.iter().skip(l-3)]; } This gives me the error error[E0308]: mismatched types --> <anon>:4:28 | 4 | vec![v.iter().take(3), v.iter().skip(l-3)]; | ^^^^^^^^^^^^^^^^^^ expected struct `std::iter::Take`, found struct `std::iter::Skip` <anon>:4:5: 4:48 note: in this expansion of vec! (defined in

Separate TcpStream + SslStream into read and write components

自古美人都是妖i 提交于 2020-05-12 08:29:06
问题 I'm trying to make client program that communicates with a server using a TcpStream wrapped by a openssl::ssl::SslStream (from crates.io). It should wait for read , and process data sent from the server if it was received without delay . At the same time, it should be able to send messages to the server regardless of reading. I tried some methods such as Passing single stream to both read and write threads. Both read and write methods require a mutable reference, so I couldn't pass a single

What's the difference between &mut unsafe { } and unsafe { &mut }?

北慕城南 提交于 2020-05-10 12:37:58
问题 I want to convert *mut pointer to &mut reference. // Both setting a value to ptr and getting a value from ptr succeeds. let ptr: &mut usize = unsafe { &mut *(VIRTUAL_ADDRESS_TO_ACCESS_FREE_PAGE as *mut usize) }; This works. However, if &mut is outside of unsafe block, the code will not work partially. *ptr = foo will not store foo to the memory ptr points, but let foo = *ptr will assign the value of *ptr to foo . // Setting a value to ptr fails, but getting a value from ptr succeeds. let ptr: