rust

How do I create a random String by sampling from alphanumeric characters?

☆樱花仙子☆ 提交于 2020-02-22 06:11:27
问题 I tried to compile the following code: extern crate rand; // 0.6 use rand::Rng; fn main() { rand::thread_rng() .gen_ascii_chars() .take(10) .collect::<String>(); } but cargo build says: warning: unused import: `rand::Rng` --> src/main.rs:2:5 | 2 | use rand::Rng; | ^^^^^^^^^ | = note: #[warn(unused_imports)] on by default error[E0599]: no method named `gen_ascii_chars` found for type `rand::prelude::ThreadRng` in the current scope --> src/main.rs:6:10 | 6 | .gen_ascii_chars() | ^^^^^^^^^^^^^^^

How do I create a random String by sampling from alphanumeric characters?

只谈情不闲聊 提交于 2020-02-22 06:11:00
问题 I tried to compile the following code: extern crate rand; // 0.6 use rand::Rng; fn main() { rand::thread_rng() .gen_ascii_chars() .take(10) .collect::<String>(); } but cargo build says: warning: unused import: `rand::Rng` --> src/main.rs:2:5 | 2 | use rand::Rng; | ^^^^^^^^^ | = note: #[warn(unused_imports)] on by default error[E0599]: no method named `gen_ascii_chars` found for type `rand::prelude::ThreadRng` in the current scope --> src/main.rs:6:10 | 6 | .gen_ascii_chars() | ^^^^^^^^^^^^^^^

How do I create a random String by sampling from alphanumeric characters?

风格不统一 提交于 2020-02-22 06:09:44
问题 I tried to compile the following code: extern crate rand; // 0.6 use rand::Rng; fn main() { rand::thread_rng() .gen_ascii_chars() .take(10) .collect::<String>(); } but cargo build says: warning: unused import: `rand::Rng` --> src/main.rs:2:5 | 2 | use rand::Rng; | ^^^^^^^^^ | = note: #[warn(unused_imports)] on by default error[E0599]: no method named `gen_ascii_chars` found for type `rand::prelude::ThreadRng` in the current scope --> src/main.rs:6:10 | 6 | .gen_ascii_chars() | ^^^^^^^^^^^^^^^

How do I create a random String by sampling from alphanumeric characters?

感情迁移 提交于 2020-02-22 06:09:05
问题 I tried to compile the following code: extern crate rand; // 0.6 use rand::Rng; fn main() { rand::thread_rng() .gen_ascii_chars() .take(10) .collect::<String>(); } but cargo build says: warning: unused import: `rand::Rng` --> src/main.rs:2:5 | 2 | use rand::Rng; | ^^^^^^^^^ | = note: #[warn(unused_imports)] on by default error[E0599]: no method named `gen_ascii_chars` found for type `rand::prelude::ThreadRng` in the current scope --> src/main.rs:6:10 | 6 | .gen_ascii_chars() | ^^^^^^^^^^^^^^^

Cannot resolve T: serde::Deserialize<'a> when deriving Deserialize on a generic struct

折月煮酒 提交于 2020-02-21 21:55:52
问题 I'm trying to write a struct that derives serde::Deserialize but it also has a field that should derive serde::Deserialize : extern crate serde; #[macro_use] extern crate serde_derive; use serde::{Deserialize, Serialize}; #[derive(PartialEq, Serialize, Deserialize)] pub struct Record<'a, T> where T: 'a + Serialize + Deserialize<'a>, { id: &'a str, created_at: &'a str, created_by: Option<&'a str>, last_updated_at: Option<&'a str>, object: &'a T, } impl<'a, T> Record<'a, T> where T: 'a +

初学rust——I/O Project

主宰稳场 提交于 2020-02-21 11:24:27
今天是学习rust的第五天,学习材料为官网的《The Rust Programming Language》,本笔记的内容为第12章,I/O project Chapter 12 I/O project: Building a Command Line Program Separation of Concerns for Binary Projects 将代码程序分为 main.rs 和 lib.rs ,将程序的逻辑logic移动到 lib.rs 当parsing逻辑足够小时可以放在 main.rs 下 main.rs 中的函数应该有以下功能: 调用命令行parsing logic 设置其他任何的配置 调用 lib.rs 中的run函数 若run出现错误,则处理错误 src/main.rs use std : : env ; //这个库不支持错误的Unicode编码,如果想要支持则需要std::env::arg_os use std : : process ; use minigrep : : Config ; fn main ( ) { let args : Vec < String > = env : : args ( ) . collect ( ) ; //使得命令行工具得以读取任意命令,并且将其传递、转换为一个vector //println!("{:?}", args);

How to use a Rust async fn that takes a reference as a callback?

喜夏-厌秋 提交于 2020-02-20 07:35:06
问题 async fn returns an anonymous type that implements Future , so if we want to use it as a callback, we need to convert the return value to a trait object. I tried to write an function to do this, but I had some lifetime problems. async fn will return lifetime of all parameters, so the signature of callback also needs to. How can I add the lifetime to the return value of the callback? use futures::future::{Future, FutureExt, LocalBoxFuture}; type Context = (); type AsyncCb = Box<dyn for<'r>

How to do polymorphic IO from either a File or stdin in Rust?

。_饼干妹妹 提交于 2020-02-20 06:44:48
问题 I'm trying to implement a "polymorphic" Input enum which hides whether we're reading from a file or from a stdin. More concretely, I'm trying build an enum that will have a lines method that will in turn "delegate" that call to either a File wrapped into a BufReader or to a StdInLock (both of which have the lines() method). Here's the enum: enum Input<'a> { Console(std::io::StdinLock<'a>), File(std::io::BufReader<std::fs::File>) } I have three methods: from_arg for deciding whether we're

How to do polymorphic IO from either a File or stdin in Rust?

我与影子孤独终老i 提交于 2020-02-20 06:44:03
问题 I'm trying to implement a "polymorphic" Input enum which hides whether we're reading from a file or from a stdin. More concretely, I'm trying build an enum that will have a lines method that will in turn "delegate" that call to either a File wrapped into a BufReader or to a StdInLock (both of which have the lines() method). Here's the enum: enum Input<'a> { Console(std::io::StdinLock<'a>), File(std::io::BufReader<std::fs::File>) } I have three methods: from_arg for deciding whether we're

Rust笔记

假装没事ソ 提交于 2020-02-19 11:48:02
Rust笔记 文章目录 Rust笔记 Rust基本概念 Rust 数据类型 标量类型 复合类型 Rust函数 函数定义 函数参数 函数体 语句与表达式 函数返回值 控制流 if表达式 使用 else if 实现多重条件 在 let 语句中使用 if Rust基本概念 在rust中变量默认是不可变的,如果需要定义可变变量需要使用关键字 mut 进行声明 fn main() { let a = 10; //a变量是一个不可变变量 let mut b = 10; //b变量是一个可变变量 println!("{}",b); println!("{}", a); } 禁止试改不可变变量的值 let a = 10; a = 45; //error :试图修改不可变变量的值 常量 常量类似于不可变变量,但是不可变变量可以通过使用 mut 关键字使变量可变,而常量永远不可变并且不可使用 mut 关键字修饰常量。 const MAX_INT:i32 = 100; println!("{}", MAX_INT); 变量隐藏 我们可以定义一个与之前变量重名的新变量, 而新变量会 隐藏 之前的变量。 Rustacean 们称之为第一个变量被第二个 隐藏 了, 这意味着使用这个变量时会看到第二个值。 可以用相同变量名称来隐藏它自己, 以及重复使用 let 关键字来多次隐藏 fn main(){ let a