小编JCo*_*eau的帖子

我应该用 ; 结束一个表达式吗?在一个循环内?

我刚开始学习 Rust,我经常有一些我找不到任何答案的问题。我真的不知道如何以及在哪里发布我的问题,所以我会尝试。

我开始阅读网站上的 Rust 文档,并做了猜谜游戏示例。

我意识到match cmp循环内的表达式可以变成一个语句,一切仍然有效。所以我想知道为什么和哪个版本应该是首选?

use rand::Rng;
use std::cmp::Ordering;
use std::io;

fn main() {
    println!("Guess the number!");

    let secret_number = rand::thread_rng().gen_range(1, 101);

    loop {
        println!("Please input your guess.");

        let mut guess = String::new();

        io::stdin()
            .read_line(&mut guess)
            .expect("Failed to read line");

        let guess: u32 = match guess.trim().parse() {
            Ok(num) => num,
            Err(_) => continue,
        };

        println!("You guessed: {}", guess);

        match guess.cmp(&secret_number) {
            Ordering::Less => println!("Too small!"),
            Ordering::Greater => println!("Too big!"),
            Ordering::Equal => {
                println!("You win!");
                break;
            } …
Run Code Online (Sandbox Code Playgroud)

rust

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

尝试在模板中使用组件函数时出现错误“TypeError: ... is not a function”

我有一个组件如下,我试图动态提供我的 svg viewBox 维度,从我的 bootstrap 注入到 main.ts。

import {Component} from 'angular2/core';
import {CircleComponent} from './circle.component';
import {Circles} from './circles.service';

@Component({
    selector: 'my-app',
    styleUrls: ['./app/app.component.css'],
    directives: [CircleComponent],
    providers: [Circles],    
    template: `
        <svg [attr.viewBox]="getViewBox()" preserveAspectRatio="xMidYMid meet">
            <svg:g my-circle *ngFor="#circle of circles.circles" [circle]="circle" />
        </svg>
    `
})
export class AppComponent{
    static parameters = [Circles, 'canvasWidth', 'canvasHeight'];

    constructor(circles: Circles, canvasWidth: Number, canvasHeight: Number) {
        this.circles = circles;
        this.width = canvasWidth;
        this.height = canvasHeight;
    }    

    function getViewBox(): String {
        return `0 0 ${this.width} ${this.height}`;
    } …
Run Code Online (Sandbox Code Playgroud)

typescript angular

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

标签 统计

angular ×1

rust ×1

typescript ×1