rust

Rust println! problem - weird behavior inside the println macro [duplicate]

若如初见. 提交于 2021-01-29 17:50:15
问题 This question already has answers here : Why does my string not match when reading user input from stdin? (3 answers) Closed 1 year ago . I am currently working on a simple "user input" program. The user can enter a number, which I get with std::io::stdin().read_line(&mut let_name_here).ok().expect("Error"); . After getting the user input I want to print it to the console for a review. I have noticed strange behavior within the println! macro. The following code println!("Your input: {}", let

How to pass data to openGL functions correctly in Rust

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-29 17:44:59
问题 I'm trying to abstract openGL in Rust. I use glutin to communicate with openGL (here is the code I started with). I'm having problems abstracting Uniforms in fact they randomly get -1 as location (the same code sometimes works and sometimes doesn't). I checked if the program was in use and it was so I started wondering if there is a problem in the way I send the data to openGL: // value is initialised here let name = name.to_string(); let location; unsafe { location = gl.GetUniformLocation

Where is the lldb executable vscode uses?

孤人 提交于 2021-01-29 17:10:52
问题 I'm having difficulty with debugging rust in vscode (It can't evaluate any expressions involving functions). I noticed that I don't have lldb installed (Ubuntu 20.04), but the debugger is still running. Where is it finding the lldb executable? Can I change the path? 来源: https://stackoverflow.com/questions/65542559/where-is-the-lldb-executable-vscode-uses

The proper ownership for “caching proxy” in Rust?

不羁的心 提交于 2021-01-29 15:48:25
问题 I'd like to use Factory to build an object from the String and have multiple impls: 1) actual building and 2) caching (stores in-memory in HashMap ). The problem is that in case #1 it have to pass the ownership and in case #2 HashMap owns the value and a reference can be returned only. use std::collections::HashMap; // product interface pub trait TProduct { fn get_title(&self) -> &String; } // and concrete impls pub struct ConcreteProduct1 { } impl TProduct for ConcreteProduct1 { // ... } pub

How to take multiple inputs from standard in in Rust?

随声附和 提交于 2021-01-29 14:05:05
问题 I want to take 2 user inputs, height and width: fn main() { let mut w = String::new(); let mut l = String::new(); println!("Enter the width"); io::stdin().read_line(&mut w).expect("failed to read input"); let w: i32 = w.trim().parse().expect("invalid input"); println!("Enter the length"); io::stdin().read_line(&mut l).expect("failed to read input"); let l: i32 = l.trim().parse().expect("invalid input"); println!("width {:?}", w); println!("length{:?}", l); } Is there a shorter way to achieve

Mutable borrow from HashMap and lifetime elision

a 夏天 提交于 2021-01-29 13:23:39
问题 Chapter 13 of the Rust book (2nd edition) contains an example of a simple calculation cache. Cacher takes the calculation function as a constructor param, and will cache the result - after the first call, it will not invoke the function again, but simply return the cached result: struct Cacher<T> where T: Fn(u32) -> u32, { calculation: T, value: Option<u32>, } impl<T> Cacher<T> where T: Fn(u32) -> u32, { fn new(calculation: T) -> Cacher<T> { Cacher { calculation, value: None, } } fn value(

What is the correct error part of the Result type of a function propagating different error types?

假如想象 提交于 2021-01-29 13:12:31
问题 I tried to write a function, that shall propagate different error types. Only for example see the following code: use std::fs::File; use std::io; use std::io::Read; fn main() { let number = read_number_from_file().unwrap(); println!("Read number {}!", number); } // So what is the correct error part of the Result<i32, ...>? fn read_number_from_file() -> Result<i32, io::Error> { let mut f = File::open("hello.txt")?; let mut s = String::new(); f.read_to_string(&mut s)?; let number = s.parse()?;

How to delete item from Hashmap inside a RefCell within an RwLock-ed Struct

眉间皱痕 提交于 2021-01-29 13:11:08
问题 I have a struct: pub struct CommunityContents { pub friends: RefCell<HashMap<FriendID, FriendData>>, pub index: RefCell<HashMap<u64, BTreeMap<FriendID, FriendData>>>, pub authenticated: bool, pub age: u64, pub height: u64, } Which is protected with a RwLock with a parent struct: pub struct Community { pub community_contents: RwLock<CommunityContents>, } pub struct FriendData { pointer: Rc<Data>, } pub struct Data { pub key: Key, pub friend_ids: Vec<FriendID>, } I want to be able to modify the

Borrowed value does not live long enough with a Tokio future

∥☆過路亽.° 提交于 2021-01-29 12:53:30
问题 I'm trying to write a simple HTTP server using Rust and tokio. Everything works fine until I want to send the response. The code is the following: use std::fs; use std::sync::Arc; use tokio::net::TcpListener; // 0.1.15 use tokio::prelude::*; fn main() { let addr = "0.0.0.0:8080".parse().unwrap(); let listener = TcpListener::bind(&addr).expect("unable to bind TCP listener"); let incoming = listener.incoming(); let server = incoming .map_err(|e| eprintln!("accept failed = {:?}", e)) .for_each(

JavaScript string is empty when passed to Rust WebAssembly module

心不动则不痛 提交于 2021-01-29 12:30:54
问题 When passing a string to a Rust WASM module, the passed data shows up as blank, as per the pattern matching in the real_code::compute function The following code is what I've tried. I don't know if it has to do with how its being returned, but when I pass a hardcoded &str , it works fine. However, the JsInteropString shows as blank. Here is how I encoded the string before sending it to WASM (from Passing a JavaScript string to a Rust function compiled to WebAssembly) const memory = new