在1.0之前的Rust版本中,我能够使用from_str()转换String为SocketAddr,但该函数不再存在.我怎么能在Rust 1.0中做到这一点.
let server_details = reader.read_line().ok().expect("Something went wrong").as_slice().trim();
let server: SocketAddr = from_str(server_details);
let mut s = BufferedStream::new((TcpStream::connect(server).unwrap()));
Run Code Online (Sandbox Code Playgroud) 我想从 stdin 读取一行并将其存储在字符串变量中,并将字符串值解析为 u32 整数值。read_line() 方法读取 2 个额外的 UTF-8 值,这会导致 parse 方法崩溃。
以下是我尝试删除回车符和换行符的内容:
use std::io;
use std::str;
fn main() {
let mut input = String::new();
match io::stdin().read_line(&mut input) {
Ok(n) => {
println!("{:?}", input.as_bytes());
println!("{}", str::from_utf8(&input.as_bytes()[0..n-2]).unwrap());
}
Err(e) => {
println!("{:?}", e);
}
}
}
Run Code Online (Sandbox Code Playgroud)
在 Rust 中执行此操作最惯用的方法是什么?
在学习Rust的练习中,我正在尝试一个接受你的名字的简单程序,然后打印你的名字,如果它是有效的.
只有"Alice"和"Bob"是有效名称.
use std::io;
fn main() {
println!("What's your name?");
let mut name = String::new();
io::stdin().read_line(&mut name)
.ok()
.expect("Failed to read line");
greet(&name);
}
fn greet(name: &str) {
match name {
"Alice" => println!("Your name is Alice"),
"Bob" => println!("Your name is Bob"),
_ => println!("Invalid name: {}", name),
}
}
Run Code Online (Sandbox Code Playgroud)
当我cargo run这个main.rs文件,我得到:
What's your name?
Alice
Invalid name: Alice
Run Code Online (Sandbox Code Playgroud)
现在,我的猜测是,因为"爱丽丝"属于类型&'static str且name属于类型&str,可能它没有正确匹配...
我正在制作一个简单的控制台程序,可以在Celsius和Fahrenheit之间进行转换.该程序多次获取用户输入,一次用于转换类型,一次用于转换值.当我运行程序时,它编译并运行没有错误,但是,它实际上不返回任何值.
这是程序:
use std::io;
// C to F: F = C*(9/5) + 32
// F to C: C = (F-32)*(5/9)
/**********Converts between Fahrenheit and Celsius*********/
fn main() -> () {
println!("Do you want to convert to Celsius or Fahrenheit? Input C or F");
let mut convert_type = String::new();
io::stdin()
.read_line(&mut convert_type)
.expect("Failed to conversion type.");
let t = String::from(convert_type);
println!("You want to convert to: {}", t);
println!("What temperature would you like to convert?");
let mut temp = String::new();
io::stdin()
.read_line(&mut …Run Code Online (Sandbox Code Playgroud) 我刚开始尝试 Rust,所以我实现了 Book 的“猜谜游戏”的修改版本。每次我尝试输入数字时,程序都无法从字符串解析整数:
Guess the number!
Input your guess:
50
You guessed 50
(4 bytes)!
thread 'main' panicked at 'Wrong number format!: ParseIntError { kind: InvalidDigit }', src\libcore\result.rs:997:5
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
error: process didn't exit successfully: `target\release\experiment.exe` (exit code: 101)
Run Code Online (Sandbox Code Playgroud)
下面应用了代码。
我已经尝试过其他整数类型。
use std::io;
use rand::Rng;
fn main() {
println!("Guess the number!\nInput your guess:");
let mut guess = String::new();
let secnum = rand::thread_rng().gen_range(1,101);
'br: loop {
match io::stdin().read_line(&mut guess) {
Ok(okay) => …Run Code Online (Sandbox Code Playgroud) 在 Rust 中将 String 类型值解析为浮点类型值时,一切都可以正常工作"let pi: f64 = 3.14".parse().unwrap();。
然而,当解析来自标准输入的 String 类型值时,即使它是完全相同的值,程序也会出现恐慌并抛出:
线程“main”在“调用
Result::unwrap()一个Err值:ParseFloatError { kind:Invalid }”时惊慌失措,src/libcore/result.rs:999:5 注意:使用RUST_BACKTRACE=1环境变量运行以显示回溯。
我检查了值的类型,它是一个字符串,所以我不明白错误是什么,并且我无法找到与标准输入(stdin)和此问题相关的任何内容。还有其他人遇到过这个吗?有没有什么好的办法可以避免恐慌呢?
这是一些复制该问题的代码:
use std::io::{stdin,stdout,Write};
fn main() {
let mut s = String::new();
println!("Give a number ");
stdin().read_line(&mut s)
.expect("Did not enter a correct string");
let user_input: f64 = s.parse().unwrap();
println!("{:?}", user_input)
}
Run Code Online (Sandbox Code Playgroud)
提前致谢!
在下面的代码中,我希望当用户输入"q"时打印消息"哇",但事实并非如此.
fn main() {
let mut input = String::new();
io::stdin().read_line(&mut input)
.expect("failed to read line");
if input == "q" {
println!("wow") ;
}
}
Run Code Online (Sandbox Code Playgroud)
为什么消息未按预期打印?
我正在尝试match使用以下代码处理用户提供的字符串:
use std::io;
fn main() {
let mut input = String::new();
io::stdin().read_line(&mut input).expect("Failed to read line.");
match input.as_ref(){
"test" => println!("That was test"),
_ => print!("Something's wrong"),
}
}
Run Code Online (Sandbox Code Playgroud)
然而,即使我输入“test”,这段代码也总是打印“Something's error”。我怎样才能使这项工作按预期进行?