rust

Split a string and return Vec<String>

孤人 提交于 2020-07-13 10:02:42
问题 I want to split a string and return Vec<String> from my function. It has to be Vec<String> and not Vec<&str> because I can't return Vec<&str> , can I? If I can, though, how can I do that? let var1: Vec<&str> = my_string.split("something").collect(); let res = var1.iter().map(|x| x.to_string()); // I want to return Vec<String> I've tried different versions but gotten error: mismatched types and other kinds of similar errors. Is there an easier way? 回答1: You don't need to create an intermediate

How should I structure a web project that has a server, client and shared data between them? [closed]

冷暖自知 提交于 2020-07-10 10:24:21
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 18 hours ago . Improve this question I want to understand how to structure web-project that has server, client and shared between them data (folder/lib or whatever). I will use actix-web for the server and yew for the client. The structure should be something like ├── server │ ├── Cargo

Using rug in Rust with wasm-bindgen

偶尔善良 提交于 2020-07-10 07:44:43
问题 I'd like to use the rug crate with wasm-bindgen to allow me to use arbitrary-precision arithmetic in a web browser. Whenever I try to compile, though, it fails to build the library, saying something along the lines of unresolved imports libc::c_char , libc::c_int , libc::c_long . Is there a workaround for this? If Emscripten is involved that's fine, but I'd like to be able to mostly write Rust code. 回答1: rug depends on libc I found https://github.com/rust-lang/libc/issues/858#issuecomment

Is there any way of doing unwrap_or_return an Error (any error)

℡╲_俬逩灬. 提交于 2020-07-10 06:57:18
问题 Is there any way to simplify the returns in the following example (originally copied from here): use std::num::ParseIntError; fn multiply(first_number_str: &str, second_number_str: &str) -> Result<i32, ParseIntError> { let first_number = match first_number_str.parse::<i32>() { Ok(first_number) => first_number, Err(e) => return Err(e), }; let second_number = match second_number_str.parse::<i32>() { Ok(second_number) => second_number, Err(e) => return Err(AnotherError::ParseError("error")), };

Is there any way of doing unwrap_or_return an Error (any error)

一笑奈何 提交于 2020-07-10 06:57:10
问题 Is there any way to simplify the returns in the following example (originally copied from here): use std::num::ParseIntError; fn multiply(first_number_str: &str, second_number_str: &str) -> Result<i32, ParseIntError> { let first_number = match first_number_str.parse::<i32>() { Ok(first_number) => first_number, Err(e) => return Err(e), }; let second_number = match second_number_str.parse::<i32>() { Ok(second_number) => second_number, Err(e) => return Err(AnotherError::ParseError("error")), };

How to store an invariant type variable in Rust

人盡茶涼 提交于 2020-07-10 06:47:08
问题 I would like to parse the type of each value in a row of data from tokio-postgresql Here is an example of getting a single value for a row of data from postgresql: ... let rows = client .query("select * from ExampleTable;") .await?; // This is how you read a string if you know the first column is a string type. let thisValue: &str = rows[0].get(0); In this example, it is known at design-time that the type in the first column is a string, and therefore the type for thisValue is &str . I would

How to store an invariant type variable in Rust

时光毁灭记忆、已成空白 提交于 2020-07-10 06:46:27
问题 I would like to parse the type of each value in a row of data from tokio-postgresql Here is an example of getting a single value for a row of data from postgresql: ... let rows = client .query("select * from ExampleTable;") .await?; // This is how you read a string if you know the first column is a string type. let thisValue: &str = rows[0].get(0); In this example, it is known at design-time that the type in the first column is a string, and therefore the type for thisValue is &str . I would

How can I read a file line-by-line, eliminate duplicates, then write back to the same file?

二次信任 提交于 2020-07-09 11:36:10
问题 I want to read a file, eliminate all duplicates and write the rest back into the file - like a duplicate cleaner. Vec because a normal array has a fixed size but my .txt is flexible (am I doing this right?). Read, lines in Vec + delete duplices: Missing write back to file. use std::io; fn main() { let path = Path::new("test.txt"); let mut file = io::BufferedReader::new(io::File::open(&path, R)); let mut lines: Vec<String> = file.lines().map(|x| x.unwrap()).collect(); // dedup() deletes all

How to use a COM VARIANT in Rust winapi programming?

柔情痞子 提交于 2020-07-09 09:42:39
问题 I'm trying to convert the C++ COM code for TaskScheduler to Rust and am stuck with the VARIANT argument of ITaskService::Connect : extern crate winapi; use winapi::{ ctypes::c_void, shared::{ guiddef::{GUID, REFCLSID, REFIID}, ntdef::{HRESULT, NULL}, rpcdce::{RPC_C_AUTHN_LEVEL_PKT_PRIVACY, RPC_C_IMP_LEVEL_IMPERSONATE}, winerror::FAILED, wtypes::VARENUM, wtypesbase::CLSCTX_INPROC_SERVER, }, um::{ combaseapi::{CoCreateInstance, CoInitializeEx, CoInitializeSecurity, CoUninitialize}, oaidl,

Filtering files or directories discovered with fs::read_dir()

两盒软妹~` 提交于 2020-07-09 08:56:06
问题 I've got this function: fn folders(dir: &Path) -> Result<Vec<PathBuf>, io::Error> { fs::read_dir(dir)? .into_iter() .map(|x| x.map(|entry| entry.path())) .collect() } It's actually borrowed from here. The function is OK; unfortunately, I don't really understand how it works. Ok(["/home/ey/dir-src/9", "/home/ey/dir-src/11", "/home/ey/dir-src/03 A Letter of Explanation.mp3", "/home/ey/dir-src/02 Egyptian Avenue.mp3", "/home/ey/dir-src/alfa", "/home/ey/dir-src/10"]) The test output shows both