rust

Why can't I call gen_range with two i32 arguments?

白昼怎懂夜的黑 提交于 2020-12-27 07:14:09
问题 I have this code but it doesn't compile: use rand::Rng; use std::io; fn main() { println!("Guess the number!"); let secret_number = rand::thread_rng().gen_range(0, 101); println!("The secret number is: {}", secret_number); println!("Please input your guess."); let mut guess = String::new(); io::stdin() .read_line(&mut guess) .expect("Failed to read line"); println!("You guessed: {}", guess); } Compile error: error[E0061]: this function takes 1 argument but 2 arguments were supplied --> src

How to implement the Debug trait for struct that has a trait object member? [duplicate]

眉间皱痕 提交于 2020-12-26 09:16:45
问题 This question already has answers here : Can I get a trait object of a multi-trait instance without using a generic type? (1 answer) Is there any way to create a type alias for multiple traits? (2 answers) Closed last year . My goal is printing the contents of struct that has trait object member but I can't find how to tell Rust compiler that the member also implements other traits like Display or Debug . For example, in the following program, I want to print the structure of S2 (and S1 for

How to implement the Debug trait for struct that has a trait object member? [duplicate]

心不动则不痛 提交于 2020-12-26 09:15:15
问题 This question already has answers here : Can I get a trait object of a multi-trait instance without using a generic type? (1 answer) Is there any way to create a type alias for multiple traits? (2 answers) Closed last year . My goal is printing the contents of struct that has trait object member but I can't find how to tell Rust compiler that the member also implements other traits like Display or Debug . For example, in the following program, I want to print the structure of S2 (and S1 for

How to implement the Debug trait for struct that has a trait object member? [duplicate]

假装没事ソ 提交于 2020-12-26 09:14:56
问题 This question already has answers here : Can I get a trait object of a multi-trait instance without using a generic type? (1 answer) Is there any way to create a type alias for multiple traits? (2 answers) Closed last year . My goal is printing the contents of struct that has trait object member but I can't find how to tell Rust compiler that the member also implements other traits like Display or Debug . For example, in the following program, I want to print the structure of S2 (and S1 for

Can a trait have a supertrait that is parameterized by a generic?

倖福魔咒の 提交于 2020-12-26 08:32:28
问题 Can you do something like this in Rust? trait A : forall<T> B<T> { ... } That is, if we want: impl A for D { ... } We must first implement: impl<T> B<T> for D { ... } 回答1: No. Rust's type system doesn't currently support any features involving higher kinded types. It does, however, support a similar construction to what you described, but limited to lifetime parameters. For example: trait B<'a> {} trait A: for<'a> B<'a> {} struct D; impl A for D { } This is an error: error[E0277]: the trait

How to tell if something is heap or stack allocated?

坚强是说给别人听的谎言 提交于 2020-12-26 05:57:11
问题 I wonder if there's a way to figure out if a variable is stack or heap allocated. Consider this: struct SomeStruct; fn main() { let some_thing = Box::new(SomeStruct); println!("{:p}", some_thing); foo(&*some_thing); } fn foo (bar: &SomeStruct) { println!("{:p}", bar); } prints 0x1 0x1 And then struct SomeStruct; fn main() { let some_thing = &SomeStruct; println!("{:p}", some_thing); foo(some_thing); } fn foo (bar: &SomeStruct) { println!("{:p}", bar); } prints 0x10694dcc0 0x10694dcc0 I can

Why am I getting “unused Result which must be used … Result may be an Err variant, which should be handled” even though I am handling it?

放肆的年华 提交于 2020-12-26 05:56:07
问题 fn main() { foo().map_err(|err| println!("{:?}", err)); } fn foo() -> Result<(), std::io::Error> { Ok(()) } results: warning: unused `std::result::Result` that must be used --> src/main.rs:2:5 | 2 | foo().map_err(|err| println!("{:?}", err)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: #[warn(unused_must_use)] on by default = note: this `Result` may be an `Err` variant, which should be handled Finished dev [unoptimized + debuginfo] target(s) in 0.58s Running `target/debug

How do I change the default rustc / Cargo linker?

假如想象 提交于 2020-12-26 05:51:54
问题 I would like to make rustc use lld as a linker instead of ld in a particular crate. So I create .cargo/config in my project directory with the following: [target.x86_64-unknown-linux-gnu] linker = "ld.lld" Which leads to linker errors: $ cargo build ... = note: ld.lld: error: unable to find library -ldl ld.lld: error: unable to find library -lrt ld.lld: error: unable to find library -lpthread ld.lld: error: unable to find library -lgcc_s ld.lld: error: unable to find library -lc ld.lld: error

How do I change the default rustc / Cargo linker?

邮差的信 提交于 2020-12-26 05:51:05
问题 I would like to make rustc use lld as a linker instead of ld in a particular crate. So I create .cargo/config in my project directory with the following: [target.x86_64-unknown-linux-gnu] linker = "ld.lld" Which leads to linker errors: $ cargo build ... = note: ld.lld: error: unable to find library -ldl ld.lld: error: unable to find library -lrt ld.lld: error: unable to find library -lpthread ld.lld: error: unable to find library -lgcc_s ld.lld: error: unable to find library -lc ld.lld: error

Disable default constructor in Rust?

最后都变了- 提交于 2020-12-26 05:19:57
问题 Say I define my own type in a Rust library, like so: struct Date { year: u16, month: u8, day: u8 } impl Date { fn new(y: u16, m: u8, d: u8) -> Date { // Do some validation here first Date { year: y, month: m, day: d } } } Is there a way to require users to use the Date::new constructor? In other words, can I somehow prohibit users from creating their own Date struct with the default constructor like so: let d = Date { 2017, 7, 10 }; I ask because it seems to be a detrimental flaw if you