Linked Questions
87 questions linked to/from What are the differences between Rust's `String` and `str`?
26 votes
1 answer
25k views
Matching string in Rust [duplicate]
I'm new to Rust (1.31) and I would like to understand a simple piece of code that does not compile: fn main() { s = String::from("foo"); match s { "foo" => { println!("...
10 votes
2 answers
2k views
Why does str primarily exist in it's borrowed form? [duplicate]
This is how the str type is used: let hello = "Hello, world!"; // with an explicit type annotation let hello: &'static str = "Hello, world!"; let hello: str = "Hello, world!"; leads to expected `...
3 votes
1 answer
4k views
Rust - How are 'String' and 'str' different in memory? [duplicate]
I have seen this question, but still didn't get it, so here's what I know (it might be incorrect): Initializing a variable of type String allocates memory on the heap and stores a pointer to that ...
12 votes
0 answers
15k views
How to declare a const String in stable Rust? [duplicate]
I am trying to declare a const String in stable Rust but it does not let me declare it: const CONSTANT_VALUE: String = String::from("constant value"); fn main() { println!("{}", TARGET_PORT_KEY);...
7 votes
1 answer
2k views
str::contains doesn't work when the supplied input is a string reference [duplicate]
use std::collections::HashSet; fn character_count(needles: &HashSet<char>, needle_type: &str, input: &str) -> i32 { for needle in needles { let mut needle_custom; ...
0 votes
1 answer
2k views
How use global static const string? [duplicate]
There is a ALPHABET for arithmetic, it's const and used in a lot of functions. I want make the ALPHABET global, static, and const. Some functions need the ALPHABET type as &String, but str....
1 vote
1 answer
739 views
What's the difference between str and String::from in this example? [duplicate]
What's the difference between str and String::from in this example use std::borrow::Cow; fn main() { let s = "Hello world!"; let cow: Cow<str> = Cow::Owned(s); // ...
1 vote
2 answers
2k views
Why does Err("some error") put its message "some error" as long as the program lives by 'static lifetime [duplicate]
I've come across a piece of code that returns Err("some errors") with a return type of Result<T, &'static str>. This confuses me. Why does it use 'static str? I know that static ...
-2 votes
1 answer
2k views
Return Vec<str> from function [duplicate]
If I do fn split(text: String) -> Vec<str> { It tells me this 29 | fn split(text: String) -> Vec<str> { | ^^^^^^^^ doesn't have a size known at ...
1 vote
0 answers
1k views
What's the difference between &String and &str in a function signature? [duplicate]
Considering the following program: fn main() { let s = String::from("helloworld"); // This works fine println!("The first word is: {}", first_word(&s)); // This ...
0 votes
1 answer
588 views
Return HashMap type from function [duplicate]
In the following function, I am creating a new HashMap with entries from a file (divided in name & value -> I am trying to read a meminfo file from Linux). I am stuck on how to return the ...
-3 votes
2 answers
363 views
rust try to return a unknow length of string [duplicate]
I am try to convert a int to binary with 000 in the front, my code is fn int32_to_binerystr(i: &i32, width: &usize) -> str { let mut s = format!("{:032b}", i); let from = s....
1 vote
1 answer
108 views
Rust difference between creating variables [duplicate]
I started learning rust and I'm unsure what is the difference between creating variables like this let mut word = String::new(); and let mut word = ""; and also what is a variable which ...
1 vote
1 answer
127 views
Isn't there a way to give a value to a vector without any "strings attached" / without borrowing? [duplicate]
Need help to make this work. I thought clone() would make a deep copy of my string. Then getting the literal-string of it would just be a value ... not linked to any variable? fn main() { let mut ...
1 vote
0 answers
160 views
what is the difference between String::from and mut x = "some string" in rust [duplicate]
my understanding is that declaring a string variable like the following let x = "a string" allocates memory for immutable x on the stack while declaring x like this let x = String::from("a string");...