Why can’t I store a value and a reference to that value in the same struct?

Let’s look at a simple implementation of this: struct Parent { count: u32, } struct Child<‘a> { parent: &’a Parent, } struct Combined<‘a> { parent: Parent, child: Child<‘a>, } impl<‘a> Combined<‘a> { fn new() -> Self { let parent = Parent { count: 42 }; let child = Child { parent: &parent }; Combined { … Read more

How to match a String against string literals?

UPDATE: Use .as_str() like this to convert the String to an &str: match stringthing.as_str() { “a” => println!(“0”), “b” => println!(“1”), “c” => println!(“2”), _ => println!(“something else!”), } Reason .as_str() is more concise and enforces stricter type checking. The trait as_ref is implemented for multiple types and its behaviour could be changed for type … Read more

Convert a String to int?

You can directly convert to an int using the str::parse::<T>() method, which returns a Result containing the int. let my_string = “27”.to_string(); // `parse()` works with `&str` and `String`! let my_int = my_string.parse::<i32>().unwrap(); You can either specify the type to parse to with the turbofish operator (::<>) as shown above or via explicit type annotation: … Read more

How do I print in Rust the type of a variable?

You can use the std::any::type_name function. This doesn’t need a nightly compiler or an external crate, and the results are quite correct: fn print_type_of<T>(_: &T) { println!(“{}”, std::any::type_name::<T>()) } fn main() { let s = “Hello”; let i = 42; print_type_of(&s); // &str print_type_of(&i); // i32 print_type_of(&main); // playground::main print_type_of(&print_type_of::<i32>); // playground::print_type_of<i32> print_type_of(&{ || “Hi!” … Read more

Why does the Rust compiler not optimize code assuming that two mutable references cannot alias?

Rust originally did enable LLVM’s noalias attribute, but this caused miscompiled code. When all supported LLVM versions no longer miscompile the code, it will be re-enabled. If you add -Zmutable-noalias=yes to the compiler options, you get the expected assembly: adds: mov eax, dword ptr [rsi] add eax, eax add dword ptr [rdi], eax ret Simply … Read more

What is the difference between iter and into_iter?

TL;DR: The iterator returned by into_iter may yield any of T, &T or &mut T, depending on the context. The iterator returned by iter will yield &T, by convention. The iterator returned by iter_mut will yield &mut T, by convention. The first question is: “What is into_iter?” into_iter comes from the IntoIterator trait: pub trait … Read more

How to disable unused code warnings in Rust?

You can either: Add an allow attribute on a struct, module, function, etc.: #[allow(dead_code)] struct SemanticDirection; Add a crate-level allow attribute; notice the !: #![allow(dead_code)] Pass it to rustc: rustc -A dead_code main.rs Pass it using cargo via the RUSTFLAGS environment variable: RUSTFLAGS=”$RUSTFLAGS -A dead_code” cargo build

How do I concatenate strings?

When you concatenate strings, you need to allocate memory to store the result. The easiest to start with is String and &str: fn main() { let mut owned_string: String = “hello “.to_owned(); let borrowed_string: &str = “world”; owned_string.push_str(borrowed_string); println!(“{}”, owned_string); } Here, we have an owned string that we can mutate. This is efficient as … Read more

Why doesn’t println! work in Rust unit tests?

This happens because Rust test programs hide the stdout of successful tests in order for the test output to be tidy. You can disable this behavior by passing the –nocapture option to the test binary or to cargo test (but, in this case after — – see below): #[test] fn test() { println!(“Hidden output”) } … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)