rust

Conditionally implement a Rust trait only if a type constraint is satisfied

北慕城南 提交于 2020-01-05 05:55:17
问题 I have the following struct: pub struct Foo<T> { some_value: T, } impl<T> Foo<T> { pub fn new(value: T) -> Self { Self { some_value: value } } } // Implement `Default()`, assuming that the underlying stored type // `T` also implements `Default`. impl<T> Default for Foo<T> where T: Default, { fn default() -> Self { Self::new(T::default()) } } I would like Foo::default() to be available if T implements Default , but not available otherwise. Is it possible to specify "conditional implementation"

Conditionally implement a Rust trait only if a type constraint is satisfied

岁酱吖の 提交于 2020-01-05 05:55:05
问题 I have the following struct: pub struct Foo<T> { some_value: T, } impl<T> Foo<T> { pub fn new(value: T) -> Self { Self { some_value: value } } } // Implement `Default()`, assuming that the underlying stored type // `T` also implements `Default`. impl<T> Default for Foo<T> where T: Default, { fn default() -> Self { Self::new(T::default()) } } I would like Foo::default() to be available if T implements Default , but not available otherwise. Is it possible to specify "conditional implementation"

Is using the type `[my_struct]` the correct way of passing an array of C structs to a Rust function?

社会主义新天地 提交于 2020-01-05 05:46:04
问题 C file: typedef struct point { int x; int y; } point; typedef struct points { int count; point *array_of_points; } points; Rust file: #[derive(Debug)] #[repr(C)] pub struct point { x: c_int, y: c_int, } #[derive(Debug)] #[repr(C)] pub struct points { count: c_int, array_of_points: [point], } #[no_mangle] pub fn do_something(all_points: &points) { for i in 0..all_points.count { let crr_point = &all_points.array_of_points[i as usize]; println!("{:?}", crr_point); } } In my C file, I allocate a

Why is a reference variable accessed via auto-deref moved?

牧云@^-^@ 提交于 2020-01-05 04:32:06
问题 I thought I got the idea of move semantics until this code. fn main() { let v = Data { body: vec![10, 40, 30], }; p(&v); } fn p(d: &Data) { for i in d.body { // &d.body, Why d.body move? println!("{}", i); } } struct Data { body: Vec<i32>, } error[E0507]: cannot move out of borrowed content --> src/main.rs:9:14 | 9 | for i in d.body { | ^^^^^^ cannot move out of borrowed content error[E0507]: cannot move out of `d.body` which is behind a `&` reference --> src/main.rs:9:14 | 8 | fn p(d: &Data)

Error E0201 when implementing foreign trait for local type with parameter

我们两清 提交于 2020-01-05 04:17:05
问题 I'm trying to add the C type parameter to this code (playground): use std::ops::Index; struct ConnectionHandle(usize); struct Connection<C>(C); impl<C> Index<ConnectionHandle> for Vec<Connection<C>> { type Output = Connection<C>; fn index(&self, ch: ConnectionHandle) -> &Self::Output { &self[ch.0] } } But doing so causes this error message: error[E0210]: type parameter `C` must be used as the type parameter for some local type (e.g. `MyStruct<C>`) --> src/lib.rs:6:1 | 6 | impl<C> Index

How to use two pointers to iterate a linked list in Rust?

大兔子大兔子 提交于 2020-01-05 03:26:29
问题 I just started to learn Rust lang and tried to do some practices on Leetcode. I'm working the problem Middle of the Linked List. The solution is just to use the slow and fast pointer. This is my code in Rust: #[derive(PartialEq, Eq, Debug)] pub struct ListNode { pub val: i32, pub next: Option<Box<ListNode>> } impl ListNode { #[inline] pub fn new(val: i32) -> Self { ListNode { next: None, val } } } struct Solution; impl Solution { pub fn middle_node(head: Option<Box<ListNode>>) -> Option<Box

Confusion about Rust HashMap and String borrowing

女生的网名这么多〃 提交于 2020-01-05 03:04:52
问题 This program accepts an integer N, followed by N lines containing two strings separated by a space. I want to put those lines into a HashMap using the first string as the key and the second string as the value: use std::collections::HashMap; use std::io; fn main() { let mut input = String::new(); io::stdin().read_line(&mut input) .expect("unable to read line"); let desc_num: u32 = match input.trim().parse() { Ok(num) => num, Err(_) => panic!("unable to parse") }; let mut map = HashMap::<&str,

Confusion about Rust HashMap and String borrowing

好久不见. 提交于 2020-01-05 03:04:42
问题 This program accepts an integer N, followed by N lines containing two strings separated by a space. I want to put those lines into a HashMap using the first string as the key and the second string as the value: use std::collections::HashMap; use std::io; fn main() { let mut input = String::new(); io::stdin().read_line(&mut input) .expect("unable to read line"); let desc_num: u32 = match input.trim().parse() { Ok(num) => num, Err(_) => panic!("unable to parse") }; let mut map = HashMap::<&str,

Textfile-parsing function fails to compile owing to lifetime/borrow error

安稳与你 提交于 2020-01-05 02:54:07
问题 NB. This post was originally part of a larger post that contained two questions (that I'd believed were one error manifesting itself differently), but to comply with site guidelines I've split it into two separate posts, of which this is the second. The first post is here. I'm trying to parse a simple config text file, which contains one three-word entry per line, laid out as follows: ITEM name value ITEM name value //etc. I've reproduced the function which does the parsing (and the

Why does my canonicalized path get prefixed with \\?\

杀马特。学长 韩版系。学妹 提交于 2020-01-05 02:27:31
问题 I'm working on a personal project that I was trying to solve via canonicalizing a relative path in Rust. However, whenever I do so, the new path gets prefixed with a strange \\?\ sequence. For example, something as simple as: let p = fs::canonicalize(".").unwrap(); println!("{}", p.display()); will result in something like the following output: \\?\C:\Users\[...]\rustprojects\projectname This isn't a particular problem because I can accomplish what I'm attempting in other ways. However, it