rust

How to define mutual recursion with closures?

↘锁芯ラ 提交于 2020-06-08 19:02:48
问题 I can do something like this: fn func() -> (Vec<i32>, Vec<i32>) { let mut u = vec![0;5]; let mut v = vec![0;5]; fn foo(u: &mut [i32], v: &mut [i32], i: usize, j: usize) { for k in i+1..u.len() { u[k] += 1; bar(u, v, k, j); } } fn bar(u: &mut [i32], v: &mut [i32], i: usize, j: usize) { for k in j+1..v.len() { v[k] += 1; foo(u, v, i, k); } } foo(&mut u, &mut v, 0, 0); (u,v) } fn main() { let (u,v) = func(); println!("{:?}", u); println!("{:?}", v); } but I would prefer to do something like this

How to get the current cursor position in file?

删除回忆录丶 提交于 2020-06-08 18:27:31
问题 Given this code: let any_offset: u64 = 42; let mut file = File::open("/home/user/file").unwrap(); file.seek(SeekFrom::Start(any_offset)); // println!("{:?}", file.cursor_position()) How can I obtain the current cursor position? 回答1: You should call Seek:seek with a relative offset of 0. This has no side effect and returns the information you are looking for. Seek is implemented for a number of types, including: impl Seek for File impl<'_> Seek for &'_ File impl<'_, S: Seek + ?Sized> Seek for

How to get the current cursor position in file?

旧巷老猫 提交于 2020-06-08 18:27:10
问题 Given this code: let any_offset: u64 = 42; let mut file = File::open("/home/user/file").unwrap(); file.seek(SeekFrom::Start(any_offset)); // println!("{:?}", file.cursor_position()) How can I obtain the current cursor position? 回答1: You should call Seek:seek with a relative offset of 0. This has no side effect and returns the information you are looking for. Seek is implemented for a number of types, including: impl Seek for File impl<'_> Seek for &'_ File impl<'_, S: Seek + ?Sized> Seek for

What is the idiomatic way of using an if-let binding when matching a `Result` and still being able to capture the error?

不羁岁月 提交于 2020-06-08 17:45:08
问题 fn lines_from_file<F>(filename: F) -> Result<io::Lines<BufReader<File>>, io::Error> where F: std::convert::AsRef<std::path::Path>, { let file = File::open(filename)?; Ok(io::BufReader::new(file).lines()) } fn main() { let filename: &str = "input.pdl"; // This works fine match lines_from_file(filename) { Ok(lines) => { for line in lines { println!("{:?}", line); }, } Err(e) => println!("Error {:?}", e), } } I'd like to use this instead: if let lines = Ok(lines_from_file(filename)) { for line

What is an idiomatic way to collect an iterator of &T into a collection of Ts?

会有一股神秘感。 提交于 2020-06-07 06:38:24
问题 I need to collect an iterator over a slice of &str s into a collection of &str s. The problem is that the iterator yields &&str s. I tried to map from &word to word , and while it works, I don't know if it is considered good or if there are better options available. The problem: use std::collections::HashSet; fn main() { let words = &["hello", "world", "I'm", "a", "Rustacean!"]; let hashset = words.iter().collect::<HashSet<&str>>(); } Playground error[E0277]: a collection of type `std:

What is an idiomatic way to collect an iterator of &T into a collection of Ts?

泪湿孤枕 提交于 2020-06-07 06:37:26
问题 I need to collect an iterator over a slice of &str s into a collection of &str s. The problem is that the iterator yields &&str s. I tried to map from &word to word , and while it works, I don't know if it is considered good or if there are better options available. The problem: use std::collections::HashSet; fn main() { let words = &["hello", "world", "I'm", "a", "Rustacean!"]; let hashset = words.iter().collect::<HashSet<&str>>(); } Playground error[E0277]: a collection of type `std:

How can I store an async function in a struct and call it from a struct instance?

£可爱£侵袭症+ 提交于 2020-06-07 04:41:09
问题 I'm trying to achieve this with the new async / await syntax, std::future::Future s and a recent version of Tokio. I'm using Tokio 0.2.0-alpha.4 and Rust 1.39.0-nightly . Different things I've tried include: using Box<dyn> s for the types that I want to store in the struct using generics in the struct definition I couldn't quite get a minimal working version, so here's a simplified version of what I'm trying to achieve: async fn foo(x: u8) -> u8 { 2 * x } // type StorableAsyncFn = Fn(u8) ->

How do I tell Cargo to run files from a directory other than “src”?

你说的曾经没有我的故事 提交于 2020-06-03 16:13:10
问题 I have a front-end project that has a lot of stuff in src folder, and I have the opportunity to also use Rust on the server side. All my Rust server files are in the server folder; how can I tell Cargo to run ./server/app.rs ? 回答1: As stated in the comments, you are probably better off just moving all of your code into the "server" directory. If you don't, you are going to be swimming uphill against defaults, which is not usually a great idea. That being said, you can specify the path to the

How do I tell Cargo to run files from a directory other than “src”?

给你一囗甜甜゛ 提交于 2020-06-03 16:11:40
问题 I have a front-end project that has a lot of stuff in src folder, and I have the opportunity to also use Rust on the server side. All my Rust server files are in the server folder; how can I tell Cargo to run ./server/app.rs ? 回答1: As stated in the comments, you are probably better off just moving all of your code into the "server" directory. If you don't, you are going to be swimming uphill against defaults, which is not usually a great idea. That being said, you can specify the path to the

Rust E0597 - borrowed value does not live long enough

戏子无情 提交于 2020-06-01 06:07:30
问题 Alright so, I'm about a month in on Rust and doing more mundane tasks in it just for exercise. I came across the calamine crate to read in data from excel. I thought I was well on my way to understand burrowing and ownership, but this one is new and even reading some other examples and looking in the docs didn't help explain it or I at least haven't came across it. So a basic for loop for row in r.rows() { let writer1 = row[11].to_string(); if let Some(cap) = exp.captures(&writer1) { //