Skip to content

Commit 6fa88f5

Browse files
committed
added strings example
1 parent 6e7c6db commit 6fa88f5

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ in a controlled repository now.
88
* `datatypes`: examples of various datatype thingies
99
* `structs.rs`: struct and enum examples
1010
* `arrays.rs`: array and vector examples
11+
* `strings.rs`: string and str examples

strings.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
fn main() {
2+
let mut s = String::new();
3+
s.push('a');
4+
s.push('b');
5+
println!("{}", s);
6+
7+
let t = 5u32.to_string();
8+
println!("{}", t);
9+
let t = "ab".to_string();
10+
assert_eq!(s, t);
11+
12+
let u = format!("{}{}", 'a', 'b');
13+
assert_eq!(s, u);
14+
15+
/*
16+
for i in 0..s.len() {
17+
println!("{}", s[i]);
18+
}
19+
*/
20+
21+
for c in s.chars() {
22+
println!("{}", c);
23+
}
24+
25+
let w: &str = "ab";
26+
assert_eq!(s, w);
27+
28+
let w: &str = &s;
29+
println!("{}", w);
30+
}

0 commit comments

Comments
 (0)