Recent Entries 4
- snippet critical 112d agoHow do I print output without a trailing newline?The macro `println!` in Rust always leaves a newline character at the end of each output. For example ``` println!("Enter the number: "); io::stdin().read_line(&mut num); ``` gives the output `Enter the number: 56 ` I don't want the user's input `56` to be on a new line. How do I do this?
- snippet critical 112d agoHow to print a Vec?I tried the following code: ``` fn main() { let v2 = vec![1; 10]; println!("{}", v2); } ``` But the compiler complains: `error[E0277]: `std::vec::Vec` doesn't implement `std::fmt::Display` --> src/main.rs:3:20 | 3 | println!("{}", v2); | ^^ `std::vec::Vec` cannot be formatted with the default formatter | = help: the trait `std::fmt::Display` is not implemented for `std::vec::Vec` = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead = note: required by `std::fmt::Display::fmt` ` Does anyone implement this trait for `Vec`?
- pattern critical 112d agoWhy doesn't println! work in Rust unit tests?I've implemented the following method and unit test: `use std::fs::File; use std::path::Path; use std::io::prelude::*; fn read_file(path: &Path) { let mut file = File::open(path).unwrap(); let mut contents = String::new(); file.read_to_string(&mut contents).unwrap(); println!("{contents}"); } #[test] fn test_read_file() { let path = &Path::new("/etc/hosts"); println!("{path:?}"); read_file(path); } ` I run the unit test this way: `rustc --test app.rs; ./app ` I could also run this with `cargo test ` I get a message back saying the test passed but the `println!` is never displayed on screen. Why not?
- gotcha critical 112d agoDifference between fmt.Println() and println() in GoAs illustrated below, both `fmt.Println()` and `println()` give same output in Go: `Hello world!` But: how do they differ from each other? Snippet 1, using the `fmt` package; `package main import ( "fmt" ) func main() { fmt.Println("Hello world!") } ` Snippet 2, without the `fmt` package; `package main func main() { println("Hello world!") } `