type-conversions/as #173
Replies: 14 comments 9 replies
-
/* 我的答案
} 2.....很抱歉,第2题不会
} 3.................................................................................
} 4...........................................................................
} 5.................................................................... |
Beta Was this translation helpful? Give feedback.
-
第2题
} |
Beta Was this translation helpful? Give feedback.
-
第1题// 修复错误,填空
// 不要移除任何代码
fn main() {
let decimal = 97.123_f32;
let integer: u8 = decimal as u8;
let c1: char = integer as char;
let c2 = integer as char;
assert_eq!(integer, 'a' as u8);
println!("Success!")
} 第2题fn main() {
assert_eq!(u8::MAX, 255);
// 如上所示,u8 类型允许的最大值是 255.
// 因此以下代码会报溢出的错误: literal out of range for `u8`.
// **请仔细查看相应的编译错误,从中寻找到解决的办法**
// **不要修改 main 中的任何代码**
#[allow(overflowing_literals)]
let v = 1000 as u8;
println!("Success!")
} 第3题fn main() {
assert_eq!(1000 as u16, 1000);
assert_eq!(1000_u16 as u8, (1000_u16 % (u8::MAX as u16 + 1)) as u8);
// 事实上,之前说的规则对于正整数而言,就是如下的取模
println!("1000 mod 256 is : {}", 1000 % 256);
assert_eq!(-1_i8 as u8, u8::MAX);
// 从 Rust 1.45 开始,当浮点数超出目标整数的范围时,转化会直接取正整数取值范围的最大或最小值
assert_eq!(300.1_f32 as u8, u8::MAX);
assert_eq!(-100.1_f32 as u8, u8::MIN);
// 上面的浮点数转换有一点性能损耗,如果大家对于某段代码有极致的性能要求,
// 可以考虑下面的方法,但是这些方法的结果可能会溢出并且返回一些无意义的值
// 总之,请小心使用
unsafe {
// 300.0 is 44
println!("300.0 is {}", 300.0_f32.to_int_unchecked::<u8>());
// -100.0 as u8 is 156
println!("-100.0 as u8 is {}", (-100.0_f32).to_int_unchecked::<u8>());
// nan as u8 is 0
println!("nan as u8 is {}", f32::NAN.to_int_unchecked::<u8>());
}
} 第4题// 填空
fn main() {
let mut values: [i32; 2] = [1, 2];
let p1: *mut i32 = values.as_mut_ptr();
let first_address: usize = p1 as usize;
let second_address = first_address + 4; // 4 == std::mem::size_of::<i32>()
let p2: *mut i32 = second_address as *mut i32; // p2 指向 values 数组中的第二个元素
unsafe {
// 将第二个元素加 1
*p2 += 1;
}
assert_eq!(values[1], 3);
println!("Success!")
} 第5题fn main() {
let arr :[u64; 13] = [0; 13];
assert_eq!(std::mem::size_of_val(&arr), 8 * 13);
let a: *const [u64] = &arr;
let b = a as *const [u8];
unsafe {
assert_eq!(std::mem::size_of_val(&*b), 13)
}
} |
Beta Was this translation helpful? Give feedback.
-
第四题指针没弄懂有没有大佬讲解一下 |
Beta Was this translation helpful? Give feedback.
-
第三题#[allow(overflowing_literals)]
fn main() {
/*
* 1000 i32 即 0000 0000 0000 0000 0000 0011 1110 1000
* u16截取 -> 0000 0011 1110 1000
* u8截取 -> 1110 1000 -> 1000 - 512 - 256 = 232
*/
assert_eq!(1000 as u16, 1000);
assert_eq!(1000 as u8, 232);
// 事实上,之前说的规则对于正整数而言,就是如下的取模
println!("1000 mod 256 is : {}", 1000 % 256); // =232
// 表示法: 正数 => 负数, 取反 + 1
// 1 => 0000 0001 => 1111 1110 => 1111 1111
// 是为了保证 +1 + (-1) =0
assert_eq!(-1_i8 as u8, 255);
// 从 Rust 1.45 开始,当浮点数超出目标整数的范围时,转化会直接取正整数取值范围的最大或最小值
assert_eq!(300.1_f32 as u8, 255);
assert_eq!(-100.1_f32 as u8, 0);
// 上面的浮点数转换有一点性能损耗,如果大家对于某段代码有极致的性能要求,
// 可以考虑下面的方法,但是这些方法的结果可能会溢出并且返回一些无意义的值
// 总之,请小心使用
unsafe {
// 300.0 is 44
println!("300.0 is {}", 300.0_f32.to_int_unchecked::<u8>());
// -100.0 as u8 is 156
println!("-100.0 as u8 is {}", (-100.0_f32).to_int_unchecked::<u8>());
// nan as u8 is 0
println!("nan as u8 is {}", f32::NAN.to_int_unchecked::<u8>());
}
} |
Beta Was this translation helpful? Give feedback.
-
FBI WARNING
|
Beta Was this translation helpful? Give feedback.
-
1、 fn main() {
let decimal = 97.123_f32;
let integer:u8 = decimal as u8;
let c1: char = (decimal as u8) as char;//在rust中只有u8可以转成char
let c2 = integer as char;
assert_eq!(integer, 'a' as u8);//左边的值是97对应的ascii符号是‘a’
println!("Success!")
} 2、关闭 #[allow(overflowing_literals)]
fn main() {
assert_eq!(u8::MAX, 255);
// 如上所示,u8 类型允许的最大值是 255.
// 因此以下代码会报溢出的错误: literal out of range for `u8`.
// **请仔细查看相应的编译错误,从中寻找到解决的办法**
// **不要修改 main 中的任何代码**
let v = 1000 as u8;
println!("Success!")
} 3、这题感觉也是加上前缀允许溢出后,对值的判断(可以借助编译器) #[allow(overflowing_literals)]
fn main() {
assert_eq!(1000 as u16, 1000);
assert_eq!(1000 as u8,232);
// 事实上,之前说的规则对于正整数而言,就是如下的取模
println!("1000 mod 256 is : {}", 1000 % 256);
assert_eq!(-1_i8 as u8, 255);
// 从 Rust 1.45 开始,当浮点数超出目标整数的范围时,转化会直接取正整数取值范围的最大或最小值
assert_eq!(300.1_f32 as u8, 255);
assert_eq!(-100.1_f32 as u8, 0);
// 上面的浮点数转换有一点性能损耗,如果大家对于某段代码有极致的性能要求,
// 可以考虑下面的方法,但是这些方法的结果可能会溢出并且返回一些无意义的值
// 总之,请小心使用
unsafe {
// 300.0 is 44
println!("300.0 is {}", 300.0_f32.to_int_unchecked::<u8>());
// -100.0 as u8 is 156
println!("-100.0 as u8 is {}", (-100.0_f32).to_int_unchecked::<u8>());
// nan as u8 is 0
println!("nan as u8 is {}", f32::NAN.to_int_unchecked::<u8>());
}
} 4、见教程示例 // 填空
fn main() {
let mut values: [i32; 2] = [1, 2];
let p1: *mut i32 = values.as_mut_ptr();
let first_address: usize = p1 as usize;
let second_address = first_address + 4; // 4 == std::mem::size_of::<i32>()
let p2: *mut i32 = second_address as *mut i32; // p2 指向 values 数组中的第二个元素
unsafe {
// 将第二个元素加 1
*p2+=1;
}
assert_eq!(values[1], 3);
println!("Success!")
} 5、可以用编译器报错得到答案,我的理解是rust针对数组的类型转换应用到了数组的每一个元素,即从 fn main() {
let arr :[u64; 13] = [0; 13];//[0,0,……0]
assert_eq!(std::mem::size_of_val(&arr), 8 * 13);
let a: *const [u64] = &arr;//assert_eq!(std::mem::size_of_val(&a), 8 * 2);
// unsafe{
// assert_eq!(std::mem::size_of_val(&*a), 8 * 13);
// }
let b = a as *const [u8];//u8的大小是1
unsafe {
assert_eq!(std::mem::size_of_val(&*b), 1*13)
}
} |
Beta Was this translation helpful? Give feedback.
-
#[allow(unused)]
// 修复错误,填空
// 不要移除任何代码
fn main() {
let decimal = 97.123_f32;
let integer: u8 = decimal as u8;
let c1: char = decimal as u8 as char;
let c2 = integer as char;
assert_eq!(integer+1, 'b' as u8);
println!("Success!")
}
fn main() {
assert_eq!(u8::MAX, 255);
// 如上所示,u8 类型允许的最大值是 255.
// 因此以下代码会报溢出的错误: literal out of range for `u8`.
// **请仔细查看相应的编译错误,从中寻找到解决的办法**
// **不要修改 main 中的任何代码**
let v = 1000_u16 as u8;
println!("Success!")
}
fn main() {
assert_eq!(1000 as u16, 1000);
assert_eq!(1000_u16 as u8, (1000_u16%(u8::MAX as u16 +1)) as u8);
// 事实上,之前说的规则对于正整数而言,就是如下的取模
println!("1000 mod 256 is : {}", 1000 % 256);
assert_eq!(-1_i8 as u8, u8::MAX);
// 从 Rust 1.45 开始,当浮点数超出目标整数的范围时,转化会直接取正整数取值范围的最大或最小值
assert_eq!(300.1_f32 as u8, u8::MAX);
assert_eq!(-100.1_f32 as u8, 0);
// 上面的浮点数转换有一点性能损耗,如果大家对于某段代码有极致的性能要求,
// 可以考虑下面的方法,但是这些方法的结果可能会溢出并且返回一些无意义的值
// 总之,请小心使用
unsafe {
// 300.0 is 44
println!("300.0 is {}", 300.0_f32.to_int_unchecked::<u8>());
// -100.0 as u8 is 156
println!("-100.0 as u8 is {}", (-100.0_f32).to_int_unchecked::<u8>());
// nan as u8 is 0
println!("nan as u8 is {}", f32::NAN.to_int_unchecked::<u8>());
}
}
// 填空
fn main() {
let mut values: [i32; 2] = [1, 2];
let p1: *mut i32 = values.as_mut_ptr();
let first_address: usize = p1 as usize;
let second_address = first_address + 4; // 4 == std::mem::size_of::<i32>()
let p2: *mut i32 = second_address as *mut i32; // p2 指向 values 数组中的第二个元素
unsafe {
// 将第二个元素加 1
*p2 += 1;
}
assert_eq!(values[1], 3);
println!("Success!")
}
fn main() {
let arr :[u64; 13] = [0; 13];
assert_eq!(std::mem::size_of_val(&arr), 8 * 13);
let a: *const [u64] = &arr;
let b = a as *const [u8];
unsafe {
assert_eq!(std::mem::size_of_val(&*b), 13)
}
} |
Beta Was this translation helpful? Give feedback.
-
mark finished |
Beta Was this translation helpful? Give feedback.
-
没搞明白第五题为什么最后 |
Beta Was this translation helpful? Give feedback.
-
第五题fn main() {
let arr: [u64; 13] = [0; 13]; // u64占8个字节
assert_eq!(std::mem::size_of_val(&arr), 8 * 13); // 计算给定引用所指向的值的大小
let a: *const [u64] = &arr; // 数组引用->裸指针(type1:*const T; type2: *mut T)
let b = a as *const [u8]; // 显示转换为*const[u8]裸指针
unsafe {
assert_eq!(std::mem::size_of_val(&*b), 13); // 裸指针的操作,需要放在unsafe中运行
}
} |
Beta Was this translation helpful? Give feedback.
-
type-conversions/as
Learning Rust By Practice, narrowing the gap between beginner and skilled-dev with challenging examples, exercises and projects.
https://zh.practice.rs/type-conversions/as.html
Beta Was this translation helpful? Give feedback.
All reactions