rust

Array cannot be indexed by RangeFull?

家住魔仙堡 提交于 2021-01-29 03:45:56
问题 Consider the following example: use std::ops::Index; use std::ops::RangeFull; fn f<T: Index<RangeFull>>(x: T) {} fn main() { let x: [i32; 4] = [0, 1, 2, 3]; f(x); } Upon calling f(x) , I get an error: error[E0277]: the type `[i32; 4]` cannot be indexed by `std::ops::RangeFull` --> src/main.rs:8:5 | 8 | f(x); | ^ `[i32; 4]` cannot be indexed by `std::ops::RangeFull` | = help: the trait `std::ops::Index<std::ops::RangeFull>` is not implemented for `[i32; 4]` note: required by `f` --> src/main

Understanding of reference life time in Rust

徘徊边缘 提交于 2021-01-29 00:37:05
问题 I'm a new Rust user and I'm reading a book The Complete Rust Programming Reference Guide . In the book there is an example: fn main() { let mut a = String::from("testing"); let a_ref = &mut a; a_ref.push('!'); println!("{}", a); } The book states the code will generate an error. However, on my local machine, I can run it without any issue. Is this because I'm using a newer Rust compiler [ rustc 1.41.0-nightly (412f43ac5 2019-11-24) ] and the code doesn't work on older ones? I've read some

Understanding of reference life time in Rust

我是研究僧i 提交于 2021-01-29 00:34:54
问题 I'm a new Rust user and I'm reading a book The Complete Rust Programming Reference Guide . In the book there is an example: fn main() { let mut a = String::from("testing"); let a_ref = &mut a; a_ref.push('!'); println!("{}", a); } The book states the code will generate an error. However, on my local machine, I can run it without any issue. Is this because I'm using a newer Rust compiler [ rustc 1.41.0-nightly (412f43ac5 2019-11-24) ] and the code doesn't work on older ones? I've read some

What's the difference of lifetime inference between async fn and async closure?

非 Y 不嫁゛ 提交于 2021-01-28 22:00:35
问题 Look at this code: #![feature(async_closure)] use std::future::Future; use std::pin::Pin; trait A<'a> { fn call(&'a self, data: &'a i32) -> Pin<Box<dyn 'a + Future<Output=()>>>; } impl <'a, F, Fut> A<'a> for F where Fut: 'a + Future<Output=()>, F: Fn(&'a i32) -> Fut { fn call(&'a self, data: &'a i32) -> Pin<Box<dyn 'a + Future<Output=()>>> { Box::pin(self(data)) } } async fn sample(_data: &i32) { } fn is_a(_: impl for<'a> A<'a>) { } fn main() { is_a(sample); is_a(async move |data: &i32| {

Problem with generic traits and lifetimes

巧了我就是萌 提交于 2021-01-28 21:55:36
问题 I had a problem with generic traits in a larger context and try to size it down to this smaller problem. I want to have following function: fn count<I, S, T>(pattern: T, item:I) -> usize where I: Atom, S: Iterator<Item = I> T: Atoms<I, S>, { pattern.atoms().filter(|i| i == &item).count() } The function should be passed two arguments: pattern:Atoms<...> which has a factory method atoms returning an iterator of atoms item:Atom which is the item that should be counted in the iterator of atoms

What's the difference of lifetime inference between async fn and async closure?

元气小坏坏 提交于 2021-01-28 21:46:08
问题 Look at this code: #![feature(async_closure)] use std::future::Future; use std::pin::Pin; trait A<'a> { fn call(&'a self, data: &'a i32) -> Pin<Box<dyn 'a + Future<Output=()>>>; } impl <'a, F, Fut> A<'a> for F where Fut: 'a + Future<Output=()>, F: Fn(&'a i32) -> Fut { fn call(&'a self, data: &'a i32) -> Pin<Box<dyn 'a + Future<Output=()>>> { Box::pin(self(data)) } } async fn sample(_data: &i32) { } fn is_a(_: impl for<'a> A<'a>) { } fn main() { is_a(sample); is_a(async move |data: &i32| {

Calling an unsafe library object of from a different thread

别来无恙 提交于 2021-01-28 21:22:17
问题 I am using tikv/raft-rs library for implementing a consensus system. This library has a RawNode object which is a thread-unsafe object. We must execute some functions on this object periodically (example), hence I use a new thread for executing. Here are the constraints: I need to have this object on the main-thread doesn't have this object for accessing some its internal states. (e.g.: raw_node.is_leader ) This object must be accessed on a worker thread. Because of these constraints, this

How can I get user input without receiving an “Unsed Variable” warning?

混江龙づ霸主 提交于 2021-01-28 21:03:51
问题 I'm taking a look at Rust and decided to build a small program that takes a user's input and prints it, but also want to do some math stuff with it for practice. Currently, this is how I am taking user input: let mut number = String::new(); let input = io::stdin().read_line(&mut number) .ok() .expect("Failed to read line"); println!("You entered {}", number); However, although I do get the correct input this way, Cargo gives me the following warning: src/main.rs:10:9: 10:14 warning: unused

Is there any way to allow moving a container that has a borrowed element but not dropping it?

我们两清 提交于 2021-01-28 20:59:45
问题 I have this container: use std::ptr::NonNull; struct Container { data: NonNull<u8>, } impl Container { fn new() -> Container { todo!() } fn borrow_some_heap_data<'a>(&'a self) -> &'a u8 { todo!() } fn borrow_some_heap_data_mut<'a>(&'a mut self) -> &'a mut u8 { todo!() } } impl Drop for Container { fn drop(&mut self) { todo!() } } fn main() { let container = Container::new(); let data = container.borrow_some_heap_data(); // or mut { let container = container; // move // This is safe because

Lifetime of variable in map/flat_map in Rust

时光总嘲笑我的痴心妄想 提交于 2021-01-28 20:05:00
问题 I think I have a good understanding of lifetimes, but what I've read doesn't tick with what the compiler says when it comes to closures ;) I have a function with the signature: fn split_to_words(content: &str) -> Vec<String> With for loops it looks like this: let mut acc: Vec<String> = Vec::new(); for line in content.lines() { if line.is_empty() { continue }; for word in line.split_whitespace() { acc.push(word.to_lowercase()); } } and using iterators: let acc: Vec<String> = content.lines()