How do I split a string in Rust?

Use split() let mut split = “some string 123 ffd”.split(“123”); This gives an iterator, which you can loop over, or collect() into a vector. for s in split { println!(“{}”, s) } let vec = split.collect::<Vec<&str>>(); // OR let vec: Vec<&str> = split.collect();

Why are Rust executables so huge?

Rust uses static linking to compile its programs, meaning that all libraries required by even the simplest Hello world! program will be compiled into your executable. This also includes the Rust runtime. To force Rust to dynamically link programs, use the command-line arguments -C prefer-dynamic; this will result in a much smaller file size but … Read more

Package with both a library and a binary?

Tok:tmp doug$ du -a 8 ./Cargo.toml 8 ./src/bin.rs 8 ./src/lib.rs 16 ./src Cargo.toml: [package] name = “mything” version = “0.0.1” authors = [“me <me@gmail.com>”] [lib] name = “mylib” path = “src/lib.rs” [[bin]] name = “mybin” path = “src/bin.rs” src/lib.rs: pub fn test() { println!(“Test”); } src/bin.rs: extern crate mylib; // not needed since Rust edition … Read more

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

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

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

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)