小编oto*_*ock的帖子

如何在未收到"Unsed Variable"警告的情况下获取用户输入?

我正在看看Rust,并决定构建一个小程序,它接受用户的输入并打印出来,但也希望用它做一些数学练习.目前,这是我如何获取用户输入:

let mut number = String::new();

let input = io::stdin().read_line(&mut number)
    .ok()
    .expect("Failed to read line");

println!("You entered {}", number);
Run Code Online (Sandbox Code Playgroud)

但是,虽然我确实以这种方式获得了正确的输入,但Cargo给了我以下警告:

src/inputmain.rs:10:9:10:14 警告:未使用的变量:,#[warn(unused_variables)]默认打开

src/main.rs:10 let input = reader.read_line(&mut number)

如果我只是使用input变量,无论我输入什么数字,当打印数字时,我会得到一个"2"作为回报.

我该如何避免警告?我是否有另一种方法可以在不创建2个变量绑定的情况下获取输入?

stdin input rust

1
推荐指数
1
解决办法
168
查看次数

当我使用字典条目作为变量打印字符串时,为什么要添加“可选”一词?

我正在学习Swift,并在操场上四处乱逛。我有以下字典:

var person = [
    "first": "John",
    "last": "Smith",
    "age": 21
]
Run Code Online (Sandbox Code Playgroud)

我正在使用以下行来打印输出:

"Your first name is \(person["first"]) and you are \(person["age"]) years old."
Run Code Online (Sandbox Code Playgroud)

使用此代码,我得到以下输出:

// -> "Your first name is Optional(John) and you are Optional(21) years old."
Run Code Online (Sandbox Code Playgroud)

我希望收到以下输出:

// -> "Your first name is John and you are 21 years old."
Run Code Online (Sandbox Code Playgroud)

可选来自哪里?为什么这不只是在指定键上打印值?我需要做什么来解决这个问题?

dictionary swift

1
推荐指数
1
解决办法
889
查看次数

标签 统计

dictionary ×1

input ×1

rust ×1

stdin ×1

swift ×1