rust

When do you need to use type annotations?

雨燕双飞 提交于 2021-02-16 21:34:12
问题 While reading Data Types from the Rust Book I noticed that sometimes a variable is defined with a type annotation and sometimes not. When should I use type annotations? let tup: (i32, f64, u8) = (500, 6.4, 1); let tup = (500, 6.4, 1); let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; let a: [i32; 5] = [1, 2, 3, 4, 5]; 回答1: When types have to be specified If the compiler cannot infer the type by itself, it

When do you need to use type annotations?

ぃ、小莉子 提交于 2021-02-16 21:33:52
问题 While reading Data Types from the Rust Book I noticed that sometimes a variable is defined with a type annotation and sometimes not. When should I use type annotations? let tup: (i32, f64, u8) = (500, 6.4, 1); let tup = (500, 6.4, 1); let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; let a: [i32; 5] = [1, 2, 3, 4, 5]; 回答1: When types have to be specified If the compiler cannot infer the type by itself, it

Does non-matching arm take the owner of a variable in a “match” statement in Rust?

ぃ、小莉子 提交于 2021-02-16 21:32:06
问题 I'm new to Rust. Below is my testing. #[derive(Debug)] enum Food { Cake, Pizza, Salad, } #[derive(Debug)] struct Bag { food: Food } fn main() { let bag = Bag { food: Food::Cake }; match bag.food { Food::Cake => println!("I got cake"), x => println!("I got {:?}", x) } println!("{:?}", bag); } When I run it, I got an error. error[E0382]: borrow of moved value: `bag` --> src\main.rs:20:22 | 17 | x => println!("I got {:?}", x) | - value moved here ... 20 | println!("{:?}", bag); | ^^^ value

Does non-matching arm take the owner of a variable in a “match” statement in Rust?

耗尽温柔 提交于 2021-02-16 21:30:05
问题 I'm new to Rust. Below is my testing. #[derive(Debug)] enum Food { Cake, Pizza, Salad, } #[derive(Debug)] struct Bag { food: Food } fn main() { let bag = Bag { food: Food::Cake }; match bag.food { Food::Cake => println!("I got cake"), x => println!("I got {:?}", x) } println!("{:?}", bag); } When I run it, I got an error. error[E0382]: borrow of moved value: `bag` --> src\main.rs:20:22 | 17 | x => println!("I got {:?}", x) | - value moved here ... 20 | println!("{:?}", bag); | ^^^ value

Is there a way to have a public trait in a proc-macro crate?

北战南征 提交于 2021-02-16 21:20:47
问题 I have a proc-macro crate with a macro that, when expanded, needs to use custom trait implementations for Rust built-in types. I tried to define the trait in the same crate, but Rust tells me that a proc-macro crate can only have public macros (the functions annotated with #[proc_macro] ) and nothing else can be public. So I put the trait in another crate and in the proc-macro crate included it as a dependency. But this means that anyone that wants to use my proc-macro crate has to depend on

Is there a way to have a public trait in a proc-macro crate?

孤街醉人 提交于 2021-02-16 21:20:08
问题 I have a proc-macro crate with a macro that, when expanded, needs to use custom trait implementations for Rust built-in types. I tried to define the trait in the same crate, but Rust tells me that a proc-macro crate can only have public macros (the functions annotated with #[proc_macro] ) and nothing else can be public. So I put the trait in another crate and in the proc-macro crate included it as a dependency. But this means that anyone that wants to use my proc-macro crate has to depend on

Why do I get “overflow evaluating the requirement `Sized`” when using tokio_io's read_exact with a Rc<TcpStream>?

≯℡__Kan透↙ 提交于 2021-02-16 20:11:09
问题 There are quite a few similar errors already posted: "Overflow evaluating the requirement" but that kind of recursion should not happen at all What does "Overflow evaluating the requirement" mean and how can I fix it? My case is much more simple and looks innocent: extern crate tokio_core; extern crate tokio_io; use std::{borrow::Borrow, rc::Rc}; use tokio_core::net::TcpStream; use tokio_io::io::read_exact; fn read_one(conn: Rc<TcpStream>) { read_exact(conn.borrow(), [0u8]); } It gives this

Function type vs Closure type

心不动则不痛 提交于 2021-02-16 20:07:15
问题 I want to create a vector of functions let all_rerankers = vec![ match_full , match_partial , match_regex , match_camel_case ]; However, match_camel_case needs one more parameter than other functions, so I though I could define a closure for match_camel_case // 3 is the extra parameter needed by match_camel_case let close_camel_case = |str: &str, keyword: &str| { match_camel_case(str, keyword, 3) }; and then specify the type of my vector: let all_rerankers: Vec<|str: &str, kwd: &str| ->

Function type vs Closure type

耗尽温柔 提交于 2021-02-16 20:07:01
问题 I want to create a vector of functions let all_rerankers = vec![ match_full , match_partial , match_regex , match_camel_case ]; However, match_camel_case needs one more parameter than other functions, so I though I could define a closure for match_camel_case // 3 is the extra parameter needed by match_camel_case let close_camel_case = |str: &str, keyword: &str| { match_camel_case(str, keyword, 3) }; and then specify the type of my vector: let all_rerankers: Vec<|str: &str, kwd: &str| ->

Function type vs Closure type

亡梦爱人 提交于 2021-02-16 20:06:11
问题 I want to create a vector of functions let all_rerankers = vec![ match_full , match_partial , match_regex , match_camel_case ]; However, match_camel_case needs one more parameter than other functions, so I though I could define a closure for match_camel_case // 3 is the extra parameter needed by match_camel_case let close_camel_case = |str: &str, keyword: &str| { match_camel_case(str, keyword, 3) }; and then specify the type of my vector: let all_rerankers: Vec<|str: &str, kwd: &str| ->