- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我为 Exercism 做的练习(minesweeper 任务),我需要将 usize
转换为 char
以便将其插入到 std::string::String
.
用最少的代码行描述问题:
let mut s = String::from(" ");
let mine_count: usize = 5; // This is returned from a method and will be a value between 1 and 8.
s.insert(0, _______); // So I get: "5 " at the underscores I do:
我目前这样做的方式是:
mine_count.to_string().chars().nth(0).unwrap(); // For example: '2'
或查看 full example in the Rust playground .不知何故,这并没有让我觉得优雅。
我也试过:
mine_count as char; // Where mine_count is of type u8
但是,当将 mine_count
添加到 std::string::String
时,它会变成 - 例如 - \u{2}
code> 而不是简单的 '2'
:
let mine_count: u8 = 8;
s.insert(0, mine_count as char);
println!("{:?}", s);
输出:
"\u{8} "
转载here .
是否有其他方法可以实现将1..8范围内的整数转换为单个字符(char
)的目的?
最佳答案
我建议使用 char::from_digit
连同使用它所必需的转换(as u32
):
use std::char;
fn main() {
let mut s = String::from(" ");
let mine_count: u8 = 8; // or i8 or usize
s.insert(0, char::from_digit(mine_count as u32, 10).unwrap());
println!("{:?}", s);
}
关于rust - 如何将 usize 转换为单个字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49939145/
我正在尝试在 Rust 中实现冒泡排序算法,但我遇到了类型不匹配错误。有人可以帮助实现吗? 此外,它的实现方式与我在 Python 中实现的方式相同。我确信有一种朴素的方法可以实现这一点。 fn ma
我有一些代码看起来有点像下面这样: let incoming: Vec = Vec::new(); match some_function(|data| { let temp = &mut i
我为 Exercism 做的练习(minesweeper 任务),我需要将 usize 转换为 char 以便将其插入到 std::string::String. 用最少的代码行描述问题: let m
此代码编译: fn main() { let mut s = String::from("some_string"); let n = f1(&s); s.clear();
我有一个二维向量拒绝使用 i32 进行索引值,但如果我使用 as usize 转换这些值,则有效: #[derive(Clone)] struct Color; struct Pixel {
我正在尝试编译一些较旧的 Rust 代码,在这段代码上: const SOMETHING: *const c_char = -1 as *const c_char; 我收到这个错误: error: c
我在 Rust 中有这个功能: fn printboard(board: Vec) { println!("| |{:>2$} {:>2$} {:>2$} {:>2$} {:>2$} {:>
我有一个基于usize输入返回compound duration的函数: pub fn format_dhms(seconds: usize) -> String 如果输入是6000000: prin
假设我有素数和幂的向量: let mut primes: Vec = ...; let mut powers: Vec = ...; 事实是primes.len() == powers.len() .
我有一个 let mut stack: Vec = vec![5, 7, 1]长短不一。在程序的某个时刻,我想增加 stack 的最后一个元素。一个。我试过 stack.last_mut().unwr
有时索引需要紧密打包(例如网格几何),将索引存储为 u32 而不是 usize 很有用。 有没有办法在 Rust 中索引一个向量,而不必每次都显式转换为 usize?例如: vector[some_u
我正在研究 Rust 中的一些编码挑战,其中一个问题是确定一个短语是否是全字母组合。我看到了以下实现: // Copy chars into a vector, sort and remove dup
文档说 usize 是 Operations and constants for pointer-sized unsigned integers. 在大多数情况下,我可以将 usize 替换为 u32
This code works and prints "b":此代码工作并打印“b”: fn main() { let s = "abc";
下面是测试代码: pub fn reverse_complement_seq_u8(seq: T, len: usize) -> Vec where T: std::ops::Index {
我正在学习 Rust 并阅读文档,这时我偶然发现了 isize 和 usize 数据类型。文档中提到了以下内容: The primary situation in which you’d use is
我正在尝试用 Rust 实现快速排序算法,问题是,我有一个名为 'i' 的变量,用作迭代器,但起初,它的值为 '- 1',我不能将它设置为 usize 类型,因为它是负数,但我也不能将它设置为 isi
我正在尝试用 Rust 实现快速排序算法,问题是,我有一个名为 'i' 的变量,用作迭代器,但起初,它的值为 '- 1',我不能将它设置为 usize 类型,因为它是负数,但我也不能将它设置为 isi
isize 和 usize 可以不同吗?它们都可以用于内存大小、索引、偏移量。 因为 usize 用于数组,为什么我们不只使用 usize 我是 Rust 的新手,所以这可能是一个基本问题。 更新:在
我正在做一个(可能不好的)排序算法作为练习实验。 我正在尝试获取一个未排序的 i32 列表,其中包含重复项,将其分解为一个已排序数组(各种大小)的数组,然后我可以将其有效地重新组合成一个完全排序的数组
我是一名优秀的程序员,十分优秀!