rust

How do I disambiguate associated types?

邮差的信 提交于 2020-01-14 09:17:30
问题 My current code looks like this: pub trait A {} pub trait HasA { type A: A; fn gimme_a() -> Self::A; } pub trait RichA: A {} pub trait RichHasA: HasA { type A: RichA; fn gimme_a() -> Self::A; // ... more things go here ... } pub fn main() {} My goal is to be able to use RichHasA as a HasA . The above code fails to compile with: error[E0221]: ambiguous associated type `A` in bounds of `Self` --> src/main.rs:10:21 | 3 | type A: A; | ---------- ambiguous `A` from `HasA` ... 9 | type A: RichA; |

Is casting between integers expensive?

孤者浪人 提交于 2020-01-14 07:42:27
问题 I am working on a project where I am doing a lot of index-based calculation. I have a few lines like: let mut current_x: usize = (start.x as isize + i as isize * delta_x) as usize; start.x and i are usize s and delta_x is of type isize . Most of my data is unsigned, therefore storing it signed would not make much sense. On the other hand, when I manipulate an array I am accessing a lot I have to convert everything back to usize as seen above. Is casting between integers expensive? Does it

Right way to have Function pointers in struct [duplicate]

大兔子大兔子 提交于 2020-01-14 07:37:36
问题 This question already has an answer here : How do I call a function through a member variable? (1 answer) Closed 3 years ago . I'm trying to make a struct that has a mutable function pointer. I have it setup so that the function pointer is initialized to a particular function, but rust doesn't recognize the pointer when i try to use it. i get hello.rs:24:14: 24:22 error: no method named `get_func` found for type `&Container` in the current scope hello.rs:24 self.get_func(self, key) ^~~~~~~~

Creating a vector with non-constant length

笑着哭i 提交于 2020-01-14 07:16:07
问题 Editor's note : this question was asked before Rust 1.0 and some of the assertions in the question are not necessarily true in Rust 1.0. Some answers have been updated to address both versions. I want to create a vector, but I only know the size I want the vector to be at runtime. This is how I'm doing it now (i.e. creating an empty, mutable vector, and adding vectors to it) : fn add_pairs(pairs: ~[int]) -> ~[int] { let mut result : ~[int] = ~[]; let mut i = 0; while i < pairs.len() { result

Creating a vector with non-constant length

有些话、适合烂在心里 提交于 2020-01-14 07:14:09
问题 Editor's note : this question was asked before Rust 1.0 and some of the assertions in the question are not necessarily true in Rust 1.0. Some answers have been updated to address both versions. I want to create a vector, but I only know the size I want the vector to be at runtime. This is how I'm doing it now (i.e. creating an empty, mutable vector, and adding vectors to it) : fn add_pairs(pairs: ~[int]) -> ~[int] { let mut result : ~[int] = ~[]; let mut i = 0; while i < pairs.len() { result

Can you return a Result that works with any possible error type?

醉酒当歌 提交于 2020-01-14 07:04:25
问题 I want to use multiple libraries that each have their own error types. I don't really care about each specific crate's error type and I want to use the ? idiom to use the methods of those crates that return a Result type. I don't want to unwrap the values either, that would cause a panic if it hits an error. I might just want to propagate the different errors using ? to the top and perhaps choose to deal with them or ignore them if I want. I cannot do that with a std::result::Result<T, E>

but there is no value for it to be borrowed from

陌路散爱 提交于 2020-01-14 05:44:07
问题 I have the code simplified version of which looks like the following: fn main() { method1(true); } fn method1(cond: bool) -> (&[u8], String) { let ret1 = if cond { let a = "a string in base64".as_bytes().from_base64(); a.unwrap().as_slice() } else { [] // how can I return an empty array? Should I return vector instead? }; (ret1, "aaa".to_string()) } The error: error: missing lifetime specifier [E0106] test1.rs:7 fn method1(cond: bool) -> (&[u8], String) { ^~~~~ test1.rs:7:28: 7:33 help: this

Should I pass a mutable reference or transfer ownership of a variable in the context of FFI?

假装没事ソ 提交于 2020-01-14 05:17:28
问题 I have a program that utilizes a Windows API via a C FFI (via winapi-rs). One of the functions expects a pointer to a pointer to a string as an output parameter. The function will store its result into this string. I'm using a variable of type WideCString for this string. Can I "just" pass in a mutable ref to a ref to a string into this function (inside an unsafe block) or should I rather use a functionality like .into_raw() and .from_raw() that also moves the ownership of the variable to the

How to convert generic primitive types in rust? [duplicate]

自闭症网瘾萝莉.ら 提交于 2020-01-14 03:55:15
问题 This question already has an answer here : How to cast generic types that I know to be integers? (1 answer) Closed 9 months ago . I would like to write something like the following in rust: pub struct Point<T> { pub x: T, pub y: T, } impl<T> Point<T> { pub fn from<U>(other: Point<U>) -> Point<T> { Point { x: other.x as T, y: other as T, } } } This is not possible: error[E0605]: non-primitive cast: `U` as `T` --> src/lib.rs:9:16 | 9 | x: other.x as T, | ^^^^^^^^^^^^ | = note: an `as`

Is there a way to make an immutable reference mutable?

一曲冷凌霜 提交于 2020-01-14 03:46:07
问题 I want to solve a leetcode question in Rust (Remove Nth Node From End of List). My solution uses two pointers to find the Node to remove: #[derive(PartialEq, Eq, Debug)] pub struct ListNode { pub val: i32, pub next: Option<Box<ListNode>>, } impl ListNode { #[inline] fn new(val: i32) -> Self { ListNode { next: None, val } } } // two-pointer sliding window impl Solution { pub fn remove_nth_from_end(head: Option<Box<ListNode>>, n: i32) -> Option<Box<ListNode>> { let mut dummy_head = Some(Box: