rust-by-practice/solutions/basic-types/char-bool.md

84 lines
968 B
Markdown
Raw Normal View History

1.
2022-05-01 00:08:50 -05:00
```rust
use std::mem::size_of_val;
2022-05-01 00:08:50 -05:00
fn main() {
let c1 = 'a';
2022-05-01 00:08:50 -05:00
assert_eq!(size_of_val(&c1), 4);
let c2 = '中';
2022-05-01 00:08:50 -05:00
assert_eq!(size_of_val(&c2), 4);
}
```
2.
2022-05-01 00:08:50 -05:00
```rust
fn main() {
let c1 = '中';
print_char(c1);
2022-05-01 00:08:50 -05:00
}
2022-05-01 00:08:50 -05:00
fn print_char(c: char) {
println!("{}", c);
}
```
3.
2022-05-01 00:08:50 -05:00
```rust
fn main() {
let _f: bool = false;
let t = false;
if !t {
println!("hello, world");
}
}
```
4.
2022-05-01 00:08:50 -05:00
```rust
fn main() {
let f = true;
let t = true || false;
assert_eq!(t, f);
}
```
5.
2022-05-01 00:08:50 -05:00
```rust
fn main() {
let v0: () = ();
let v = (2, 3);
assert_eq!(v0, implicitly_ret_unit())
}
fn implicitly_ret_unit() {
println!("I will returen a ()")
}
// don't use this one
fn explicitly_ret_unit() -> () {
println!("I will returen a ()")
}
```
6.
2022-05-01 00:08:50 -05:00
```rust
use std::mem::size_of_val;
2022-05-01 00:08:50 -05:00
fn main() {
let unit: () = ();
// unit type does't occupy any memeory space
assert!(size_of_val(&unit) == 0);
}
```