rust

What is RUST_BACKTRACE supposed to tell me?

做~自己de王妃 提交于 2021-02-09 12:46:00
问题 My program is panicking so I followed its advice to run RUST_BACKTRACE=1 and I get this (just a little snippet). 1: 0x800c05b5 - std::sys::imp::backtrace::tracing::imp::write::hf33ae72d0baa11ed at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:42 2: 0x800c22ed - std::panicking::default_hook::{{closure}}::h59672b733cc6a455 at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/src/libstd/panicking.rs:351 If the program

Why is implementing an external trait using my type as a parameter for an external type legal?

若如初见. 提交于 2021-02-09 11:15:15
问题 I am modifying some code to depend on rand version 0.5. At first, I was worried how I would enable generating random values of my own types with Standard , but I found out this is legal: impl ::rand::distributions::Distribution<MyType> for ::rand::distributions::Standard { // ... } Why is it legal? I thought implementing an external trait for an external type is illegal. 回答1: The entire purpose of these rules (called the orphan rules or coherence rules ) is to avoid having any conflicting

If expressions that yield either value or reference?

和自甴很熟 提交于 2021-02-09 11:12:16
问题 I have this pattern that shows up every now and then, but I haven't found a nice way to implement it "correct". What it is, is I have some variable passed into my function by reference. I don't need to mutate it, I don't need to transfer ownership, I just look at its contents. However, if the contents are in some state, replace the value with a default value. For instance say my function accepts a &Vec<String> and if the vec is empty, replace it with vec!["empty"] . One might implement that

Why do I get “cannot move out of `item` because it is borrowed” for a custom type but not a Box?

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-09 08:19:11
问题 Code: use std::collections::HashSet; use std::{mem, ptr, fmt}; use std::ops::Deref; enum Unsafety { Normal } enum ImplPolarity { Positive } struct TraitRef; struct Ty; struct ImplItem; enum ItemKind { Impl(Unsafety, ImplPolarity, Option<TraitRef>, // (optional) trait this impl implements Box<Ty>, // self ), } struct Item { node: ItemKind, } pub struct P<T: ?Sized> { ptr: Box<T> } impl<T: 'static> P<T> { pub fn unwrap(self) -> T { *self.ptr } } impl<T: ?Sized> Deref for P<T> { type Target = T;

Why do I get “cannot move out of `item` because it is borrowed” for a custom type but not a Box?

左心房为你撑大大i 提交于 2021-02-09 08:19:09
问题 Code: use std::collections::HashSet; use std::{mem, ptr, fmt}; use std::ops::Deref; enum Unsafety { Normal } enum ImplPolarity { Positive } struct TraitRef; struct Ty; struct ImplItem; enum ItemKind { Impl(Unsafety, ImplPolarity, Option<TraitRef>, // (optional) trait this impl implements Box<Ty>, // self ), } struct Item { node: ItemKind, } pub struct P<T: ?Sized> { ptr: Box<T> } impl<T: 'static> P<T> { pub fn unwrap(self) -> T { *self.ptr } } impl<T: ?Sized> Deref for P<T> { type Target = T;

Why is my for loop code slower than an iterator?

我只是一个虾纸丫 提交于 2021-02-08 20:01:27
问题 I am trying to solve the leetcode problem distribute-candies. It is easy, just find out the minimum between the candies' kinds and candies half number. Here's my solution (cost 48ms): use std::collections::HashSet; pub fn distribute_candies(candies: Vec<i32>) -> i32 { let sister_candies = (candies.len() / 2) as i32; let mut kind = 0; let mut candies_kinds = HashSet::new(); for candy in candies.into_iter() { if candies_kinds.insert(candy) { kind += 1; if kind > sister_candies { return sister

How do I make a structure generic in Rust without higher kinded type (HKT) support?

让人想犯罪 __ 提交于 2021-02-08 17:38:58
问题 I am trying to make the Iteratee structure generic so I can pass in a different parsing function and get an different Iteratee . This is the non-generic version that works: use std::io::{BufRead, BufReader}; use std::str::{from_utf8, Utf8Error}; #[derive(PartialEq, Debug)] struct Cat<'a> { name: &'a str, } fn parse<'a>(slice: &'a [u8]) -> Result<Cat<'a>, Utf8Error> { from_utf8(slice).map(|name| Cat { name: name }) } struct Iteratee<R> where R: BufRead + Sized { read: R, } impl<R> Iteratee<R>

How could rust multiply &i32 with i32?

烂漫一生 提交于 2021-02-08 16:59:43
问题 Consider this example: fn main() { let v: Vec<i32> = vec![1, 2, 3, 4, 5]; let b: i32 = (&v[2]) * 4.0; println!("product of third value with 4 is {}", b); } This fails as expected as float can't be multiplied with &i32 . error[E0277]: cannot multiply `{float}` to `&i32` --> src\main.rs:3:23 | 3 | let b: i32 = (&v[2]) * 4.0; | ^ no implementation for `&i32 * {float}` | = help: the trait `std::ops::Mul<{float}>` is not implemented for `&i32` But when I change the float to int, it works fine. fn

How could rust multiply &i32 with i32?

我怕爱的太早我们不能终老 提交于 2021-02-08 16:58:56
问题 Consider this example: fn main() { let v: Vec<i32> = vec![1, 2, 3, 4, 5]; let b: i32 = (&v[2]) * 4.0; println!("product of third value with 4 is {}", b); } This fails as expected as float can't be multiplied with &i32 . error[E0277]: cannot multiply `{float}` to `&i32` --> src\main.rs:3:23 | 3 | let b: i32 = (&v[2]) * 4.0; | ^ no implementation for `&i32 * {float}` | = help: the trait `std::ops::Mul<{float}>` is not implemented for `&i32` But when I change the float to int, it works fine. fn

How could rust multiply &i32 with i32?

大憨熊 提交于 2021-02-08 16:58:37
问题 Consider this example: fn main() { let v: Vec<i32> = vec![1, 2, 3, 4, 5]; let b: i32 = (&v[2]) * 4.0; println!("product of third value with 4 is {}", b); } This fails as expected as float can't be multiplied with &i32 . error[E0277]: cannot multiply `{float}` to `&i32` --> src\main.rs:3:23 | 3 | let b: i32 = (&v[2]) * 4.0; | ^ no implementation for `&i32 * {float}` | = help: the trait `std::ops::Mul<{float}>` is not implemented for `&i32` But when I change the float to int, it works fine. fn