rust

Why can I not reverse the result of str::split?

旧街凉风 提交于 2020-07-03 07:06:08
问题 According to the docs for Split, there is a rev method on the result of doing split on a string: fn main() { let mut length = 0; let mut mult = 1; for part in "1:30".split(":").rev() { length += mult * part.parse::<i32>().unwrap(); mult *= 60; } } I get the following error: error[E0277]: the trait bound `std::str::pattern::StrSearcher<'_, '_>: std::str::pattern::DoubleEndedSearcher<'_>` is not satisfied --> src/main.rs:4:35 | 4 | for part in "1:30".split(":").rev() { | ^^^ the trait `std::str

How to download the documentation of a crate with Cargo?

六月ゝ 毕业季﹏ 提交于 2020-07-03 06:25:03
问题 In Haskell's Cabal, one can download the documentation for a package. Is it possible with Rust's Cargo? I searched on the Internet but found nothing. 回答1: You can build the documentation for all crates that are currently specified in your Cargo.toml by using the command cargo doc A list of common Cargo commands can be found with cargo --help , and detailed information for a command can be found with cargo COMMAND --help : $ cargo doc --help cargo-doc Build a package's documentation USAGE:

How do I use Serde to (de)serialize arrays greater than 32 elements, such as [u8; 128]?

不羁的心 提交于 2020-07-03 05:55:07
问题 I have a struct containing a byte array that I would like to serialize and deserialize to and from binary, but it only works for arrays up to 32 elements. Here is my minimal example code main.rs : #[macro_use] extern crate serde_derive; extern crate serde; extern crate bincode; use bincode::{serialize, deserialize, Infinite}; const BYTECOUNT: usize = 32; // 33 and more does not work, I need 128 type DataArr = [u8; BYTECOUNT]; #[derive(Serialize, Deserialize, Debug)] struct Entry { number: i64

VS Code Rust Extension Errors

青春壹個敷衍的年華 提交于 2020-06-29 06:42:38
问题 I'm trying to figure out the reason why (and possibly fix) below errors for Cargo.toml that the Rust Extension (https://marketplace.visualstudio.com/items?itemName=rust-lang.rust) on VS Code shows. could not compile `nodrop`. error: could not compile `zero`. To learn more, run the command again with --verbose. error: could not compile `bit_field`. To learn more, run the command again with --verbose. error: could not compile `nodrop`. To learn more, run the command again with --verbose. My

VS Code Rust Extension Errors

喜欢而已 提交于 2020-06-29 06:42:06
问题 I'm trying to figure out the reason why (and possibly fix) below errors for Cargo.toml that the Rust Extension (https://marketplace.visualstudio.com/items?itemName=rust-lang.rust) on VS Code shows. could not compile `nodrop`. error: could not compile `zero`. To learn more, run the command again with --verbose. error: could not compile `bit_field`. To learn more, run the command again with --verbose. error: could not compile `nodrop`. To learn more, run the command again with --verbose. My

How to convert a tuple of references to a reference of a tuple?

本秂侑毒 提交于 2020-06-29 05:36:59
问题 I'd like to convert a tuple of references (which are all references to members of the same struct) to a reference of a tuple. I've tried to coerce them in various ways, however I wasn't able to do it without cloning. struct Bar(); struct Foo(Bar, Bar, Bar); fn main() { let a: &Foo = &Foo(Bar(), Bar(), Bar()); let b: &(Bar, Bar) = &(a.0, a.1); } error[E0507]: cannot move out of borrowed content --> src/main.rs:7:28 | 7 | let b: &(Bar, Bar) = &(a.0, a.1); | ^^^ cannot move out of borrowed

Should I store unordered values from a series with large holes in a sparse Vec or HashMap in Rust?

牧云@^-^@ 提交于 2020-06-29 04:06:32
问题 I have a file that contains millions of rows of data. Each row has a unique id and the id series are many times not in order, and can contain holes. 1, 2, 10, 6, 3, 18 for example. I want to be able to quickly access the rows by ID, so I'm thinking storing them in a HashMap could be a viable solution, but this feels like overkill when they could be stored in a Vec . Is storing them in a Vec a good solution when the holes in the series can get pretty large ( 1, 2, 3, 1000000, 1000001... and so

Closure requires unique access to lambda function

狂风中的少年 提交于 2020-06-28 08:01:46
问题 I'm learning Rust, and have implemented some simple code to experiment with closures - but am running up against an issue with the borrow-checker that I'm not sure how to resolve. When compiling the following function fn twice(x:int, f:|int| -> int) -> int { f(f(x)) } I get the following error closure requires unique access to `f` but it is already borrowed I'm working through the guide and have a moderate understanding of why the borrow-checker doesn't like this - but I'm unsure how to

Why do I need `use` statements in a submodule but not in main.rs?

不打扰是莪最后的温柔 提交于 2020-06-28 06:31:16
问题 I don't understand mod or use ; I suppose that mod will import files into the project and use will use them. I have a project with this hierarchy: . |-- Cargo.lock |-- Cargo.toml |-- src | |-- display.rs | |-- list.rs | |-- main.rs | |-- parser.rs | |-- sort.rs Why do I need use in list.rs and not in main.rs ? I use the function sorting() and print_files() in list.rs like I use the function parse() and listing() in main.rs . main.rs mod parser; // Ok mod list; // Ok mod sort; // Ok mod

Rust: Deserialize a JSON array into a very simple custom table

纵然是瞬间 提交于 2020-06-28 05:50:06
问题 I'm trying to deserialize an array of arrays (represents a table of string cells) into a custom struct in Rust with serde_json. I know that using serde_json::Value is enough for this simple case but I would like to construct a custom type. use serde::{Deserialize}; use serde_json::{self, Result}; #[derive(Deserialize, Debug)] pub struct Row { pub cells: Vec<String>, } #[derive(Deserialize, Debug)] pub struct Table { pub rows: Vec<Row>, } impl Table { pub fn new(data: &str) -> Result<Table> {