rust

Unable to find crate that is listed in [build-dependencies] section

时光总嘲笑我的痴心妄想 提交于 2020-08-04 21:49:54
问题 I try to compile my project with the command cargo build . build.rs extern crate csv; use std::path::Path; use std::fs::OpenOptions; use std::io::BufWriter; use std::io::Write; #[allow(non_snake_case)] fn processCSV(filename: &str, sourcePath: &str, enumName: &str) { println!("Generate rust source code from schema {}",filename); let mut ret: Vec<String> = Vec::new(); let mut rdr = csv::Reader::from_file(filename).unwrap().flexible(true); for record in rdr.records().map(|r| r.unwrap()) { } let

Rust格式化输出的奇淫小技巧

眉间皱痕 提交于 2020-08-04 21:14:57
写rust样例时想打印个醒目的标题,类似 /// ============== /// 顶顶顶顶 /// ============== 最开始用的时宏 macro_rules! title1x { ($msg:expr) => { println!("///=========================///"); println!("/// ///"); println!("/// {s1}", s1 = $msg); println!("/// ///"); println!("///=========================///"); }; } 还不错,不过格式不是很美观。尤其时字符长短问题 后发现 官方样例有这样的代码 // 你可以在数字左边补 0。下面语句输出 "000001"。 println!("{number:>0width$}", number=1, width=6); 多半可以拿来利用一下 经过反复各种乱试!发现了一点有用的东西 macro_rules! titlex { ($msg:expr) => { // 解释 // :后面跟要填充字符,再跟填充方向标识[>^<],>从左侧填充,<从右侧填充,^两边填充 // 中文占2个字符宽度,所以会导致填充超标,超长度。 // 含义 {s1:/^w1$} s1格式化输出,两边填充字符/,填充满w1长度

How do I build a Cacher in Rust without relying on the Copy trait?

假装没事ソ 提交于 2020-08-04 16:48:29
问题 I am trying to implement a Cacher as mentioned in Chapter 13 of the Rust book and running into trouble. My Cacher code looks like: use std::collections::HashMap; use std::hash::Hash; pub struct Cacher<T, K, V> where T: Fn(K) -> V, { calculation: T, values: HashMap<K, V>, } impl<T, K: Eq + Hash, V> Cacher<T, K, V> where T: Fn(K) -> V, { pub fn new(calculation: T) -> Cacher<T, K, V> { Cacher { calculation, values: HashMap::new(), } } pub fn value(&mut self, k: K) -> &V { let result = self

How do I build a Cacher in Rust without relying on the Copy trait?

偶尔善良 提交于 2020-08-04 16:48:07
问题 I am trying to implement a Cacher as mentioned in Chapter 13 of the Rust book and running into trouble. My Cacher code looks like: use std::collections::HashMap; use std::hash::Hash; pub struct Cacher<T, K, V> where T: Fn(K) -> V, { calculation: T, values: HashMap<K, V>, } impl<T, K: Eq + Hash, V> Cacher<T, K, V> where T: Fn(K) -> V, { pub fn new(calculation: T) -> Cacher<T, K, V> { Cacher { calculation, values: HashMap::new(), } } pub fn value(&mut self, k: K) -> &V { let result = self

How do I allocate an array at runtime in Rust?

你说的曾经没有我的故事 提交于 2020-08-04 14:49:17
问题 Once I have allocated the array, how do I manually free it? Is pointer arithmetic possible in unsafe mode? Like in C++: double *A=new double[1000]; double *p=A; int i; for(i=0; i<1000; i++) { *p=(double)i; p++; } delete[] A; Is there any equivalent code in Rust? 回答1: Based on your question, I'd recommend reading the Rust Book if you haven't done so already. Idiomatic Rust will almost never involve manually freeing memory. As for the equivalent to a dynamic array, you want Vector. Unless you

How do I allocate an array at runtime in Rust?

我的梦境 提交于 2020-08-04 14:47:09
问题 Once I have allocated the array, how do I manually free it? Is pointer arithmetic possible in unsafe mode? Like in C++: double *A=new double[1000]; double *p=A; int i; for(i=0; i<1000; i++) { *p=(double)i; p++; } delete[] A; Is there any equivalent code in Rust? 回答1: Based on your question, I'd recommend reading the Rust Book if you haven't done so already. Idiomatic Rust will almost never involve manually freeing memory. As for the equivalent to a dynamic array, you want Vector. Unless you

How do I allocate an array at runtime in Rust?

断了今生、忘了曾经 提交于 2020-08-04 14:46:12
问题 Once I have allocated the array, how do I manually free it? Is pointer arithmetic possible in unsafe mode? Like in C++: double *A=new double[1000]; double *p=A; int i; for(i=0; i<1000; i++) { *p=(double)i; p++; } delete[] A; Is there any equivalent code in Rust? 回答1: Based on your question, I'd recommend reading the Rust Book if you haven't done so already. Idiomatic Rust will almost never involve manually freeing memory. As for the equivalent to a dynamic array, you want Vector. Unless you

Rust 1.45 发布:修复了 Cast Unsoundness 并稳定了 Web 框架 Rocket 的支持

孤人 提交于 2020-08-04 09:17:15
云栖号资讯:【 点击查看更多行业资讯 】 在这里您可以找到不同行业的第一手的上云资讯,还在等什么,快来! Rust 1.45 修复了一个长期存在的浮点数强制转换问题,该问题可能导致未定义行为(undefined behaviour )异常,并稳定了流行 Web 框架 Rocket 所使用的特性。 将浮点数转换为整数时,Rest 会抛出一个 未定义行为(undefined behaviour)的已知异常。如果你对Rust 的 value proposition 有所了解的话,可能会对此感到惊讶。具体来说,下面的代码片段虽然编译时不会报错,但由于使用了 cast ( as ) 将浮点数 300 强制转换为 8 位无符号整数(仅表示值介于 0 到 255 之间的整数),在 Rust 1.44 中会抛出未定义行为的异常: fn cast(x: f32) -> u8 { x as u8 } fn main() { let f = 300.0; let x = cast(f); println!("x: {}", x); } 在底层,这个问题与 LLVM 的 fptoui 指令有关,该指令在上述情况下使用会生成一个“有毒”的值。回想一下,Rust 提供了 unsafe 关键字来标记希望 挂起Rust 安全保证的代码块。上面所示的代码片段虽然没有被标记为不安全,但它却包含了不安全的代码

How to get every subset of a vector in Rust?

喜夏-厌秋 提交于 2020-08-04 06:31:13
问题 What's the easiest/most idiomatic way to get every subset of a vector in Rust? let v = vec![1,2,3]; assert_eq!(subsets(v), [[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]); 回答1: What you're searching for is called the powerset of a vector. Here's the code to generate the powerset of a slice of a vector. fn powerset<T>(s: &[T]) -> Vec<Vec<T>> where T: Clone { (0..2usize.pow(s.len() as u32)).map(|i| { s.iter().enumerate().filter(|&(t, _)| (i >> t) % 2 == 1) .map(|(_, element)| element

What is the best way to convert an AsyncRead to a TryStream of bytes?

廉价感情. 提交于 2020-08-02 04:18:26
问题 I have an AsyncRead and want to convert it to a Stream<Item = tokio::io::Result<Bytes>> with tokio 0.2 and futures 0.3. The best I've been able to do is something like: use bytes::Bytes; // 0.4.12 use futures::stream::{Stream, TryStreamExt};; // 0.3.1 use tokio::{fs::File, io::Result}; // 0.2.4 use tokio_util::{BytesCodec, FramedRead}; // 0.2.0 #[tokio::main] async fn main() -> Result<()> { let file = File::open("some_file.txt").await?; let stream = FramedRead::new(file, BytesCodec::new())