Menu
CoddyTech

Hints Without Borrow Checking

Lesson 30 of 48 in Coddy's Python for Rust Developers course.

Rust signatures enforce type contracts before execution. Python annotations document intended types and can be checked by separate tooling, but the interpreter does not enforce them automatically. The annotated doubling function still repeats a string when called with one. Python bool is a subclass of int, so an integer instance check also accepts booleans. Use explicit runtime validation if the input contract must reject them. Type hints do not restore Rust ownership, lifetimes, or exclusive borrowing.

Rust excerpt inside main

fn twice(x: i32) -> i32 { x * 2 }
println!("{}", twice(3));
println!("{}", true as i32);

Python

def twice(x: int) -> int:
    return x * 2
print(twice(3))
print(twice("ha"))
print(isinstance(True, int))

Porting trap

A Python annotation such as list[int] is not a runtime element validator. Tests and deliberate checks remain needed at untrusted input boundaries.

Implement the specified API in the editable Python files. The locked driver.py runs the program and supplies input/output. Keep its imports and names unchanged.

challenge icon

Challenge

Beginner

Implement count_at_least(values: list[int], minimum: int) -> int. Return how many integers in the list are at least the minimum, retaining duplicates as separate observations. Preserve the list. Do not print or read input. The driver reads integer tokens on one line and a minimum on the next. It prints the count and then unchanged or changed. Keep the supplied annotations as documentation; behavioral tests do not treat annotations as runtime validation.

Your function definitions go in functions.py. The locked driver.py imports them and handles the supplied input/output and checks. You do not need to edit the driver.

Try it yourself

from functions import count_at_least

values = [int(part) for part in input().split()]
minimum = int(input())
before = values.copy()
print(count_at_least(values, minimum))
print("unchanged" if values == before else "changed")
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Python for Rust Developers

Practice on your own: Online Python compiler