rust

Generic function that return different types based on an enum input

余生长醉 提交于 2020-06-28 03:43:32
问题 I have a struct which holds registers. I want my read_register function to return a u8 for Register::V0 and Register::V1 but a u16 for Register::V2 and Register::V3 . I'm not sure how to make the function generic for over the input type. I'm getting the error match arms have incompatible types which does make sense because the types are different. struct Registers { v0: u8, v1: u8, v2: u16, v3: u16, } enum Register { V0, V1, V2, V3, } impl Registers { fn read_register<T>(&self, register:

How to return the captured variable from `FnMut` closure, which is a captor at the same time

≯℡__Kan透↙ 提交于 2020-06-27 17:27:25
问题 I have a function, collect_n , that returns a Future that repeatedly polls a futures::sync::mpsc::Receiver and collects the results into a vector, and resolves with that vector. The problem is that it consumes the Receiver , so that Receiver can't be used again. I'm trying to write a version that takes ownership of the Receiver but then returns ownership back to the caller when the returned Future resolves. Here's what I wrote: /// Like collect_n but returns the mpsc::Receiver it consumes so

Vectors borrowing and ownership [duplicate]

∥☆過路亽.° 提交于 2020-06-27 16:58:10
问题 This question already has answers here : What does it mean to pass in a vector into a `for` loop versus a reference to a vector? (1 answer) How can I solve “use of moved value” and “which does not implement the `Copy` trait”? (1 answer) What do I have to do to solve a “use of moved value” error? (3 answers) What does “cannot move out of index of” mean? (1 answer) Closed 2 months ago . This doesn't work: let vectors = vec![1, 2, 3, 4, 5, 6, 7]; for i in vectors { println!("Element is {}", i);

How can I convert a one element string into a char? [duplicate]

落花浮王杯 提交于 2020-06-27 15:39:29
问题 This question already has answers here : How do I get the first character out of a string? (3 answers) Closed 2 days ago . I need to convert a one element &str into char . I was able to come up with this solution that also works for String : fn main() { let comma: &str = ","; let my_char = comma.chars().nth(0).unwrap(); assert_eq!(my_char, ','); } Is there a better or shorter way to do it? 回答1: No huge improvements I can think of, but a few notes: You could replace .nth(0) with .next() ,

How can I display a compiler warning upon function invocation?

蓝咒 提交于 2020-06-27 14:46:08
问题 I've got a function I want to export in my module so people can use it. However, in ≈95% of cases, using it is a bad idea. /// Check whether foo is a metasyntactic variable. /// /// **Using this function is a mistake.** This function is slow, /// since checking widgets is an extremely expensive operation. /// You should be keeping track of what's what, and ideally will /// never need to use this function. /// /// If you _do_ need to use this function, please consider a refactor. pub fn test

How to switch between Rust toolchains?

我的未来我决定 提交于 2020-06-27 13:00:39
问题 rustup help toolchain lists the following sub-commands SUBCOMMANDS: list List installed toolchains install Install or update a given toolchain uninstall Uninstall a toolchain link Create a custom toolchain by symlinking to a directory help Prints this message or the help of the given subcommand(s) I have the following toolchains installed stable-x86_64-unknown-linux-gnu (default) nightly-2019-09-05-x86_64-unknown-linux-gnu nightly-x86_64-unknown-linux-gnu master I was trying to solve an issue

How to switch between Rust toolchains?

北城余情 提交于 2020-06-27 13:00:26
问题 rustup help toolchain lists the following sub-commands SUBCOMMANDS: list List installed toolchains install Install or update a given toolchain uninstall Uninstall a toolchain link Create a custom toolchain by symlinking to a directory help Prints this message or the help of the given subcommand(s) I have the following toolchains installed stable-x86_64-unknown-linux-gnu (default) nightly-2019-09-05-x86_64-unknown-linux-gnu nightly-x86_64-unknown-linux-gnu master I was trying to solve an issue

How can I flatten nested Results?

帅比萌擦擦* 提交于 2020-06-27 11:18:09
问题 I'm working with a third-party library that provides tree-based data structures that I have to use "as is". The API returns Result<T, Error> . I have to make some sequential calls and convert the error to my application's internal error. use std::error::Error; use std::fmt; pub struct Tree { branches: Vec<Tree>, } impl Tree { pub fn new(branches: Vec<Tree>) -> Self { Tree { branches } } pub fn get_branch(&self, id: usize) -> Result<&Tree, TreeError> { self.branches.get(id).ok_or(TreeError {

Rust “expected type” error prints mismatched types that are exactly the same

断了今生、忘了曾经 提交于 2020-06-27 09:59:05
问题 With nightly rust: Playground struct Foo<T, F: Fn(&T, &T) -> T> { value: T, func: F } fn main() { let lambda = |&x, &y| x + y; let foo = Foo { value: 5 as i32, func: lambda }; } Error message: Compiling playground v0.0.1 (/playground) error[E0308]: mismatched types --> src/main.rs:8:15 | 8 | let foo = Foo { | ^^^ one type is more general than the other | = note: expected type `std::ops::FnOnce<(&i32, &i32)>` found type `std::ops::FnOnce<(&i32, &i32)>` Note that the expected type and found

How to move data into multiple Rust closures?

天涯浪子 提交于 2020-06-27 09:39:26
问题 I have a two widgets in a simple GTK app: extern crate gdk; extern crate gtk; use super::desktop_entry::DesktopEntry; use gdk::enums::key; use gtk::prelude::*; pub fn launch_ui(_desktop_entries: Vec<DesktopEntry>) { gtk::init().unwrap(); let builder = gtk::Builder::new_from_string(include_str!("interface.glade")); let window: gtk::Window = builder.get_object("main_window").unwrap(); let search_entry: gtk::SearchEntry = builder.get_object("search_entry").unwrap(); let list_box: gtk::ListBox =