rust

Expected type `bool`, found type `&bool`

徘徊边缘 提交于 2020-01-30 06:41:45
问题 I would like to take a bool from a Vec<bool> and compare it in an if statement. How do I solve the following error? | 7 | if cell { | ^^^^ expected bool, found &bool | = note: expected type `bool` found type `&bool` if cell.clone() works for me but seems a bit hackisch. 回答1: take a bool from a Vec<bool> Just do that: let foo = vec![true]; if foo[0] { /* ... */ } bool implements Copy, so indexing the array will copy the value out. If you had a reference to the boolean inside the vector, you

lifetime bound on associated type is rejected although it seems valid

三世轮回 提交于 2020-01-30 05:44:08
问题 I have a piece of code that does not compile and that can be reduced to this snippet: use std::error::Error; use std::convert::TryFrom; // A trait that provides methods for parsing data into a type T. pub trait Deserializable<T> { // some methods } pub struct MyBuffer<'a> { inner: &'a [u8] } impl<'a, T> Deserializable<T> for MyBuffer<'a> where T: TryFrom<&'a [u8]>, <T as TryFrom<&'a [u8]>>::Error: Error + Sync + Send + 'static { // some methods to implement } fn main() {} The compiler rejects

Iterate over lines in a string, including the newline characters

爷,独闯天下 提交于 2020-01-30 05:16:21
问题 I need to iterate over lines in a string, but keep the newlines at the end in the strings that are yielded. There is str.lines(), but the strings it returns have the newline characters chopped off: let result: Vec<_> = "foo\nbar\n".lines().collect(); assert_eq!(result, vec!["foo", "bar"]); Here's what I need: assert_eq!(lines("foo\nbar\n"), vec!["foo\n", "bar\n"]); More test cases: assert!(lines("").is_empty()); assert_eq!(lines("f"), vec!["f"]); assert_eq!(lines("foo"), vec!["foo"]); assert

How to skip n items from inside of a iterator loop?

て烟熏妆下的殇ゞ 提交于 2020-01-30 05:15:21
问题 This code: play fn main() { let text = "abcd"; for char in text.chars() { if char == 'b' { // skip 2 chars } print!("{}", char); } // prints `abcd`, but I want `ad` } prints abcd , but I want to skip 2 chars if b was found, so that it prints ad . How do I do that? I tried to put the iterator into a variable outside the loop and manipulate that iterator within the loop, but the Borrow Checker doesn't allow that. 回答1: AFAIK you can't do that with a for loop. You will need to desugar it by hand:

Why do proc-macros have to be defined in proc-macro crate?

点点圈 提交于 2020-01-30 05:01:19
问题 I was trying to create a derive macro for my trait, to simplify some stuff. I've encountered some problems: the `#[proc_macro_derive]` attribute is only usable with crates of the `proc-macro` crate type and, after the small fix proc-macro=true : proc-macro` crate types cannot export any items other than functions tagged with `#[proc_macro_derive]` currently functions tagged with `#[proc_macro_derive]` must currently reside in the root of the crate` What is the reason for this behaviour? 回答1:

String equality in Rust: how does referencing and dereferencing work?

送分小仙女□ 提交于 2020-01-30 04:43:29
问题 As a Rust newbie, I'm working through the Project Euler problems to help me get a feel for the language. Problem 4 deals with palindromes, and I found two solutions for creating a vector of palindromes, but I'm not sure how either of them work. I'm using a vector of strings, products , that's calculated like this: let mut products = Vec::new(); for i in 100..500 { for j in 500..1000 { products.push((i * j).to_string()); } } For filtering these products to only those that are palindromic, I

Is there one-line syntax for constructing a struct that contains a reference to a temporary?

筅森魡賤 提交于 2020-01-30 02:52:12
问题 Consider the following invalid Rust code. There is one struct Foo that contains a reference to a second struct Bar : struct Foo<'a> { bar: &'a Bar, } impl<'a> Foo<'a> { fn new(bar: &'a Bar) -> Foo<'a> { Foo { bar } } } struct Bar { value: String, } impl Bar { fn empty() -> Bar { Bar { value: String::from("***"), } } } fn main() { let foo = Foo::new(&Bar::empty()); println!("{}", foo.bar.value); } The compiler does not like this: error[E0716]: temporary value dropped while borrowed --> src

RUST 0x05 Enum

一个人想着一个人 提交于 2020-01-28 07:49:02
RUST 0x05 Enum 1 定义一个Enum 如: enum IpAddrKind { V4, V6, } enum的值只能是它的变量中的一个。 Enum Values 可以像这样创建实例: let four = IpAddrKind::V4; let six = IpAddrKind::V6; enum里的变量是在其namespace下的,所以要用 :: 。这时 IpAddrKind::V4 和 IpAddrKind::V6 是同一种类型—— IpAddrKind ,所以可以像这样: fn route(ip_kind: IpAddrKind) { route(IpAddrKind::V4); route(IpAddrKind::V6); 可以这样将enum和struct组合使用: enum IpAddrKind { V4, V6, } struct IpAddr { kind: IpAddrKind, address: String, } let home = IpAddr { kind: IpAddrKind::V4, address: String::from("127.0.0.1"), }; let loopback = IpAddr { kind: IpAddrKind::V6, address: String::from("::1"), };

Rust编程之旅--(一)安装开发环境

大兔子大兔子 提交于 2020-01-28 04:40:25
Rust编程之旅 -- (一)安装开发环境 Rust版本 nightly版本   -- 测试版 beta版本     --预发布版 stable版本    --稳定版 我这里为了便于学习使用的是 nightly测试版 安装发开环境 PS : 为了便于快速下载安装建议设置代理服务 //设置环境变量 RUSTUP_DIST_SERVER ( 用于更新 toolchain ) $ export RUSTUP_DIST_SERVER = https://mirrors.ustc.edu.cn/rust-static //以及 RUSTUP_UPDATE_ROOT ( 用于更新 rustup ) $ export RUSTUP_UPDATE_ROOT = https://mirrors.ustc.edu.cn/rust-static/rustup 安装rustup $ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh 安装完成后,可在$HOME/.cargo/bin目录下查看可执行程序 设置环境变量 $ export RUST_HOME = /home/eli-m/.cargo $ export PATH = ${RUST_HOME} /bin: $PATH 设置完成后 source /etc/profile

Rust判断操作系统代码

荒凉一梦 提交于 2020-01-28 03:48:14
fn main() { if cfg!(target_os = "windows") { println!("Hello Windows"); }else if cfg!(target_os = "linux"){ println!("Hello Linux"); }else{ println!("Unknown os"); } } 来源: CSDN 作者: 勾吴江南 链接: https://blog.csdn.net/TX_OfficeDev/article/details/103795102