Linked Questions

26 votes
1 answer
25k views

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!("...
Coding thermodynamist's user avatar
10 votes
2 answers
2k views

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 `...
QurakNerd's user avatar
  • 864
3 votes
1 answer
4k views

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 ...
skevula's user avatar
  • 81
12 votes
0 answers
15k views

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);...
Akiner Alkan's user avatar
  • 7,068
7 votes
1 answer
2k views

use std::collections::HashSet; fn character_count(needles: &HashSet<char>, needle_type: &str, input: &str) -> i32 { for needle in needles { let mut needle_custom; ...
James Smith's user avatar
0 votes
1 answer
2k views

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....
Sloong's user avatar
  • 51
1 vote
1 answer
739 views

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); // ...
hewc's user avatar
  • 139
1 vote
2 answers
2k views

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 ...
En Xie's user avatar
  • 636
-2 votes
1 answer
2k views

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 ...
AMTitan's user avatar
1 vote
0 answers
1k views

Considering the following program: fn main() { let s = String::from("helloworld"); // This works fine println!("The first word is: {}", first_word(&s)); // This ...
Romstar's user avatar
  • 1,285
0 votes
1 answer
588 views

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 ...
GraXx's user avatar
  • 3
-3 votes
2 answers
363 views

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....
En Xie's user avatar
  • 636
1 vote
1 answer
108 views

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 ...
paspielka's user avatar
1 vote
1 answer
127 views

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 ...
Robert's user avatar
  • 171
1 vote
0 answers
160 views

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");...
krabbos's user avatar
  • 47

15 30 50 per page
1
2 3 4 5 6