This question already has answers here:
How do I change characters at a specific index within a string in rust?
(2 answers)
What are the differences between Rust's `String` and `str`?
(14 answers)
Closed 3 months ago.
let mut hour = "sss"
let mut min = "lll"
I want to swap hour[0] as string and min[1] as string, how can I do that?
Related
This question already has answers here:
How does Rust implement reflection?
(1 answer)
How do I print in Rust the type of a variable?
(17 answers)
Closed 8 months ago.
I can not find a built-in function to check the type of my param in this function (Rust):
pub fn notify<T: Summary + Display>(item: &T) {
// type of "item" (Summary || Display)
}
This question already has answers here:
What is the relation between auto-dereferencing and deref coercion?
(1 answer)
What are Rust's exact auto-dereferencing rules?
(4 answers)
Closed 2 years ago.
All the commented lines below are valid. How do multiple & have the same impact? Does Rust do some magic to interpret multiple &s?
// All commented code is also valid - wondering how?
let string_a = String::from("String A");
let str_a: &str = &string_a;
// let str_a: &str = &&&&&string_a; // --> this is valid statement
let mut str_option: Option<&str>;
str_option = Some(str_a);
// str_option = Some(&str_a); // --> this is valid statement
// str_option = Some(&&str_a); // --> this is also valid statement
This question already has answers here:
How to create a String directly?
(3 answers)
What is the difference between these 3 ways of declaring a string in Rust?
(1 answer)
How do I convert between String, &str, Vec<u8> and &[u8]?
(1 answer)
Closed 2 years ago.
In Rust, there are several ways to create a String from a string literal:
fn main() {
let s_from = String::from("string"); // type on the right of the operator
let s_into: String = "string".into(); // type on the left of the operator
let s_to_string = "string".to_string(); // expresses type
let s_to_owned = "string".to_owned(); // expresses ownership
assert_eq!(s_from, s_into);
assert_eq!(s_from, s_to_string);
assert_eq!(s_from, s_to_owned);
}
Is there a rule in rust to follow a reading direction in relation to the operator?
Is there a reason to favour From/Into over to_string()/to_owned()?
Is there a reason to favour one of those over all the others?
With several developers working on a project, a mixture usage of those happens.
This question already has answers here:
How can I initialize an array using a function? [duplicate]
(5 answers)
How do I collect into an array?
(10 answers)
Does Rust have a way to apply a function/method to each element in an array or vector?
(2 answers)
Closed 3 years ago.
The community reviewed whether to reopen this question last month and left it closed:
Original close reason(s) were not resolved
What is the simplest form of initializing an array with consecutive integers from 0 to N?
I have this code, but I think idiomatic Rust will look much simpler:
const NUM: u32 = 8;
fn main() {
let mut int_list: [u32; NUM as usize] = [0; NUM as usize];
for i in 0..NUM {
int_list[i as usize] = i;
}
println!("data: {:?}", int_list);
}
playground
This question already has answers here:
How can I use the format! macro in a no_std environment?
(5 answers)
A more convenient concatenation with a string literal in Rust
(3 answers)
How to format output to a byte array with no_std and no allocator?
(2 answers)
How to create a static string at compile time
(3 answers)
Closed 3 years ago.
I'm trying to concatenate two strings (&str) or convert a byte array in a string in Rust without using std. I saw core::str::from_utf8 but that's not what I'm looking for.
I'm searching something like
let b: [u8; 2] = [97, 98];
let result: &str = core::str::array_to_string(b); // "ab"
or
let a: &str = "Hello ";
let b: &str = "world !";
let result: &str = core::str::concatenate(a, b);