rust

How do I handle/circumvent “Cannot assign to … which is behind a & reference” in Rust?

时光总嘲笑我的痴心妄想 提交于 2021-02-08 04:07:50
问题 I'd implementing a simple linked list. This is the (working) code I had so far: pub struct LinkedList<T> { start: Option<Box<Link<T>>>, } impl<T> LinkedList<T> { pub fn new() -> LinkedList<T> { return LinkedList { start: None }; } } struct Link<T> { value: Box<T>, next: Option<Box<Link<T>>>, } impl<T> Link<T> { fn new_end(value: T) -> Link<T> { return Link::new(value, None); } fn new(value: T, next: Option<Box<Link<T>>>) -> Link<T> { return Link { value: Box::new(value), next, }; } } Next on

How do I handle/circumvent “Cannot assign to … which is behind a & reference” in Rust?

徘徊边缘 提交于 2021-02-08 04:05:27
问题 I'd implementing a simple linked list. This is the (working) code I had so far: pub struct LinkedList<T> { start: Option<Box<Link<T>>>, } impl<T> LinkedList<T> { pub fn new() -> LinkedList<T> { return LinkedList { start: None }; } } struct Link<T> { value: Box<T>, next: Option<Box<Link<T>>>, } impl<T> Link<T> { fn new_end(value: T) -> Link<T> { return Link::new(value, None); } fn new(value: T, next: Option<Box<Link<T>>>) -> Link<T> { return Link { value: Box::new(value), next, }; } } Next on

How do I tell which Windows toolchain my Rust compiler is using?

孤人 提交于 2021-02-08 04:01:24
问题 I'm trying to install Rust debugging tools on my Windows machine. I know I have to find if I'm using the GNU or MSVC toolchain my Rust compiler is using, but I don't know how to find this information. 回答1: The platform toolchain is known by the compiler. Use rustc --version --verbose to see it: PS C:\Users\IEUser> rustc --version --verbose rustc 1.26.0 (a77568041 2018-05-07) binary: rustc commit-hash: a7756804103447ea4e68a71ccf071e7ad8f7a03e commit-date: 2018-05-07 host: x86_64-pc-windows

How do I tell which Windows toolchain my Rust compiler is using?

大城市里の小女人 提交于 2021-02-08 04:01:05
问题 I'm trying to install Rust debugging tools on my Windows machine. I know I have to find if I'm using the GNU or MSVC toolchain my Rust compiler is using, but I don't know how to find this information. 回答1: The platform toolchain is known by the compiler. Use rustc --version --verbose to see it: PS C:\Users\IEUser> rustc --version --verbose rustc 1.26.0 (a77568041 2018-05-07) binary: rustc commit-hash: a7756804103447ea4e68a71ccf071e7ad8f7a03e commit-date: 2018-05-07 host: x86_64-pc-windows

Is it possible to pass a tuple struct constructor to a function to return different types of values?

让人想犯罪 __ 提交于 2021-02-08 02:59:11
问题 Is it possible to pass different struct constructors to a function so that it could return different values created with those constructors? For example, I might pass String10 or String20 to createString and it should create a different type of value based on the constructor passed in. I don't know how to set the type for ctor nor the return type. I tried a generic <T> without success. pub struct String10(String); pub struct String20(String); impl String10 { pub fn create(fieldName: &str, str

Converting to a Box<Any>

百般思念 提交于 2021-02-07 20:16:21
问题 I have a Box<Trait> , and want to be able to cast it to Box<Obj> . There is BoxAny to supposedly do this, but trying to call t.downcast::<Obj>() says there's not method downcast in scope. The docs show how to do this if you have a reference. You can just do &Trait as &Any . But it doesn't seem to be possible to do boxedTrait as Box<Any> . Here's a playground showing what I mean. 回答1: Any allows downcasting to a concrete type, so you need to know this concrete type when you convert to Box<Any>

Converting to a Box<Any>

空扰寡人 提交于 2021-02-07 20:15:38
问题 I have a Box<Trait> , and want to be able to cast it to Box<Obj> . There is BoxAny to supposedly do this, but trying to call t.downcast::<Obj>() says there's not method downcast in scope. The docs show how to do this if you have a reference. You can just do &Trait as &Any . But it doesn't seem to be possible to do boxedTrait as Box<Any> . Here's a playground showing what I mean. 回答1: Any allows downcasting to a concrete type, so you need to know this concrete type when you convert to Box<Any>

How do I return a Result containing every error from an iterator of Results, not just the first one?

老子叫甜甜 提交于 2021-02-07 19:34:33
问题 I'm trying to implement a simple interpreter in Rust, for which I have created a Tokens struct, which takes source characters and produces either a Token or a ScanError inside a Result : pub struct Tokens<'src> { chars: Chars<'src>, } impl<'src> Iterator for Tokens<'src> { type Item = Result<Token, ScanError>; fn next(&mut self) -> Option<Result<Token, ScanError>> { // ... } } Since Result implements FromIterator , it is simple to collect the result to either the first ScanError or a vector

How to init a constant matrix with ndarray? [duplicate]

ⅰ亾dé卋堺 提交于 2021-02-07 18:41:32
问题 This question already has answers here : How can you make a safe static singleton in Rust? (2 answers) How do I create a global, mutable singleton? (3 answers) Closed 2 years ago . I would like to have a matrix in ndarray as a constant available for other modules. Unfortunately, the construction function itself is not a constant function. Is there any way around that restriction? Code: extern crate ndarray; use ndarray::prelude::*; const foo: Array2<f32> = arr2(&[ [1.26, 0.09], [0.79, 0.92] ]

How to mutate another item in a vector, but not the vector itself, while iterating over the vector?

穿精又带淫゛_ 提交于 2021-02-07 18:17:44
问题 It is quite clear to me that iterating over a vector shouldn't let the loop body mutate the vector arbitrarily. This prevents iterator invalidation, which is prone to bugs. However, not all kinds of mutation lead to iterator invalidation. See the following example: let mut my_vec: Vec<Vec<i32>> = vec![vec![1,2], vec![3,4], vec![5,6]]; for inner in my_vec.iter_mut() { // <- or .iter() // ... my_vec[some_index].push(inner[0]); // <-- ERROR } Such a mutation does not invalidate the iterator of