rust

Is there a way to do validation as part of a filter in Warp?

梦想与她 提交于 2021-02-07 14:30:31
问题 I have a route and an endpoint function defined. I've also injected some dependencies. pub fn route1() -> BoxedFilter<(String, ParamType)> { warp::get() .and(warp::path::param()) .and(warp::filters::query::query()) .and(warp::path::end()) .boxed() } pub async fn handler1( query: String, param: ParamType, dependency: DependencyType, ) -> Result<impl warp::Reply, warp::Rejection> { } let api = api::routes::route1() .and(warp::any().map(move || dependency)) .and_then(api::hanlders::hander1);

Resolve union structure in Rust FFI

谁说我不能喝 提交于 2021-02-07 14:23:20
问题 I have problem with resolving c-union structure XEvent. I'm experimenting with Xlib and X Record Extension in Rust. I'm generate ffi-bindings with rust-bindgen. All code hosted on github alxkolm/rust-xlib-record. Trouble happen on line src/main.rs:106 when I try extract data from XEvent structure. let key_event: *mut xlib::XKeyEvent = event.xkey(); println!("KeyPress {}", (*key_event).keycode); // this always print 128 on any key My program listen key events and print out keycode . But it is

Can Rust code compile without the standard library?

瘦欲@ 提交于 2021-02-07 13:58:01
问题 I'm currently in the progress of learning Rust. I'm mainly using The Rust Programming Language book and this nice reference which relates Rust features/syntax to C++ equivalents. I'm having a hard time understanding where the core language stops and the standard library starts. I've encountered a lot of operators and/or traits which seems to have a special relationship with the compiler. For example, Rust has a trait (which from what I understand is like an interface) called Deref which let's

Do rust mutable references have move semantics?

ε祈祈猫儿з 提交于 2021-02-07 13:14:01
问题 fn main() { let mut name = String::from("Charlie"); let x = &mut name; let y = x; // x has been moved say_hello(y); say_hello(y); // but y has not been moved, it is still usable change_string(y); change_string(y); } fn say_hello(s: &str) { println!("Hello {}", s); } fn change_string(s: &mut String) { s.push_str(" Brown"); } When I assign x to y x has been moved. However, I would expect something with move semantics to be moved when I use it in a function. However, I can still use the

Do rust mutable references have move semantics?

六眼飞鱼酱① 提交于 2021-02-07 13:12:52
问题 fn main() { let mut name = String::from("Charlie"); let x = &mut name; let y = x; // x has been moved say_hello(y); say_hello(y); // but y has not been moved, it is still usable change_string(y); change_string(y); } fn say_hello(s: &str) { println!("Hello {}", s); } fn change_string(s: &mut String) { s.push_str(" Brown"); } When I assign x to y x has been moved. However, I would expect something with move semantics to be moved when I use it in a function. However, I can still use the

Can I set an LLDB breakpoint when multiple Rust source files share the same name?

白昼怎懂夜的黑 提交于 2021-02-07 12:55:25
问题 Background: In Rust, you typically have multiple source files called mod.rs . For example: app_name src main.rs foo mod.rs bar mod.rs Problem: I can't find a way to distinguish one mod.rs from another when setting an LLDB breakpoint: $ cargo build $ rust-lldb target/debug/app_name (lldb) breakpoint set -f mod.rs -l 10 Breakpoint 1: 2 locations. (lldb) breakpoint set -f foo/mod.rs -l 10 Breakpoint 2: no locations (pending). WARNING: Unable to resolve breakpoint to any actual locations. (lldb)

What is the difference between Vec<i32> and Vec<Box<i32>>?

这一生的挚爱 提交于 2021-02-07 11:55:38
问题 let vec1 = vec![1, 2, 3, 4]; let vec2 = vec![Box::new(1), Box::new(2), Box::new(3), Box::new(4)]; What is the difference between them? I have already allocated vec1 on the heap. So aren't all the elements of vec1 also on the heap? Why would I need to separately allocate them on the heap like in vec2? 回答1: I'll draw a diagram. The first value is a pointer to a contiguous array of numbers on the heap. (stack) (heap) ┌──────┐ ┌───┐ │ vec1 │──→│ 1 │ └──────┘ ├───┤ │ 2 │ ├───┤ │ 3 │ ├───┤ │ 4 │ └─

How do I use a custom comparator function with BTreeSet?

家住魔仙堡 提交于 2021-02-07 11:23:49
问题 In C++, it is possible to customize the code std::set uses to sort its arguments. By default it uses std::less, but that can be changed with the Compare template parameter. Rust's BTreeSet uses the Ord trait to sort the type. I don't know of a way to override this behavior -- it's built into the type constraint of the type stored by the container. However, it often makes sense to build a list of items that are sorted by some locally-useful metric that nevertheless is not the best way to

Why does the borrow checker disallow a second mutable borrow even if the first one is already out of scope?

你。 提交于 2021-02-07 08:12:36
问题 Background I know that the borrow checker disallows more than one mutable borrows. For example, the code below is invalid: fn main() { let mut x = 42; let a = &mut x; let b = &mut x; println!("{} {}", a, b); } However, if the first borrow is dropped due to out of scope, the second borrow is valid: fn main() { let mut x = 1; { let a = &mut x; println!("{}", a); } let b = &mut x; println!("{}", b); } Because of non-lexical lifetimes (NLL), the first borrow doesn't even have to be out of scope —

Why does the borrow checker disallow a second mutable borrow even if the first one is already out of scope?

ⅰ亾dé卋堺 提交于 2021-02-07 08:11:38
问题 Background I know that the borrow checker disallows more than one mutable borrows. For example, the code below is invalid: fn main() { let mut x = 42; let a = &mut x; let b = &mut x; println!("{} {}", a, b); } However, if the first borrow is dropped due to out of scope, the second borrow is valid: fn main() { let mut x = 1; { let a = &mut x; println!("{}", a); } let b = &mut x; println!("{}", b); } Because of non-lexical lifetimes (NLL), the first borrow doesn't even have to be out of scope —